O
O
oc1tane2014-01-07 15:13:25
linux
oc1tane, 2014-01-07 15:13:25

How to write a script in linux that generates a list of files and writes to another .txt file?

Please tell me how to write a linux script that generates a list of files in the current directory and puts it in the file file.txt. At the end of file.txt put the full name of the current directory and the creation date of file.txt. If the file file.txt existed before, it will be overwritten.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
korjavin, 2014-01-07
@oc1tane

#!/bin/sh
ls > /path/to/file.txt
pwd >> /path/to/file.txt
date >> /path/to/file.txt

X
xara, 2014-10-31
@xara

I think it would be better not to hardcode the script parameters. For example, pass the directory for listing as the first argument ($1 for bash), and do not put the output directly into a file, but give the user of the script the opportunity to direct it to the desired file (or to another script / command through a pipe).
I also have an assumption that a recursive list of files in the current directory is needed (ie find instead of ls). Usually the directory listing is not large, it would not make sense to save it to a file.

#!/bin/bash
find $1
echo -n 'Date: ';  date;
echo -n 'Listing of dir: '; pwd;

use the script through:
or you can do more complex constructions:
/path/to/dirlist.sh /home/user | gzip > user_file_listing.txt.gz

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question