Answer the question
In order to leave comments, you need to log in
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
#!/bin/sh
ls > /path/to/file.txt
pwd >> /path/to/file.txt
date >> /path/to/file.txt
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;
/path/to/dirlist.sh /home/user | gzip > user_file_listing.txt.gz
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question