How to Send List of File in a Folder to a Txt File in Linux

How to send list of file in a folder to a txt file in Linux

you can just use

ls > filenames.txt

(usually, start a shell by using "Terminal", or "shell", or "Bash".) You may need to use cd to go to that folder first, or you can ls ~/docs > filenames.txt

Print list of files in a directory to a text file (but not the text file itself) from terminal

printf '%s\n' * > output.txt

Note that this assumes that there's no preexisting output.txt file -
if so, delete it first.

  • printf '%s\n' * uses globbing (filename expansion) to robustly print the names of all files and subdirectories located in the current directory, line by line.

  • Globbing happens before output.txt is created via output redirection > output.txt (which still happens before the command is executed, which explains your problem), so its name is not included in the output.

  • Globbing also avoids the use of ls, whose use in scripting is generally discouraged.

Bash how to create a txt file of all file names in a directory

Too much work.

ls > ../log.txt

Pass a list of filenames from a .txt to a find and copy command

sed 's/\.xml//' /Sites/csv/file_list.xml > /tmp/your_jpg_files.txt
find /Images/JPG -type f -name "*.jpg" | grep -F -f /tmp/your_jpg_files.txt |
while read FILEPATH; do
mv "/Sites/csv/${FILEPATH##*/}.xml" "${FILEPATH%/*}"
done

Explanation:

sed 's/\.xml//' /Sites/csv/file_list.xml > /tmp/your_jpg_files.txt

Read the filenames from file_list.xml, remove the suffix .xml and save the result in a temp file.


find /Images/JPG -type f -name "*.jpg"

List all the paths to the .jpg files since the directory /Images/JPG


grep --fixed-strings --file=/tmp/your_jpg_files.txt

In the list of paths, you look the ones of your jpg files


while read FILEPATH; do
mv "/Sites/csv/${FILEPATH##*/}.xml" "${FILEPATH%/*}"
done

At this step, you have all the paths to the .jpg files and for any of them, you'll make the mv you want.

${FILEPATH##*/} = $(basename "$FILEPATH") but more efficient.

${FILEPATH%/*} = $(dirname "$FILEPATH") but more efficient.


Example

FILEPATH contains /Images/JPG/Day2/DF0005.jpg, so the actual mv will be:

# /Sites/csv/${FILEPATH##*/}.xml -> /Sites/csv/DF0005.jpg.xml
# ${FILEPATH%/*} -> /Images/JPG/Day2/
mv "/Sites/csv/DF0005.jpg.xml" "/Images/JPG/Day2/"

Linux: append all filenames in path to text file

  1. Create an empty sh file

  2. List the files *.cub and loop through them

  3. Store the sequence by splitting on dot [.]

  4. echo the required string and append to the sh file of step 1

     echo -n "" > 'Run.sh'
    for filename in `ls *.cub`
    do
    sequence=`echo $filename | cut -d "." -f1`
    echo "Program -i $filename -o $sequence.vdb" >> Run.sh
    done

Directly put the stream into the file as below:

    for filename in `ls *.cub`
do
sequence=`echo $filename | cut -d "." -f1`
echo "Program -i $filename -o $sequence.vdb"
done > Run.sh

For everything before the extension to be retained in the variable:

    for filename in `ls *.cub`
do
sequence=`echo $filename | rev | cut -d "." -f2- | rev`
echo "Program -i $filename -o $sequence.vdb"
done > Run.sh

For extracting only the numbers from the filename and use accordingly:

    for filename in `ls *.cub`
do
sequence=`echo $filename | sed 's/[^0-9]*//g'`
echo "Program -i $filename -o $sequence.vdb"
done > Run.sh

How to list the files inside directory and sub directories?

Try this :

find / -type f -name \*.txt

It will give you all .txt files in '/' directory.

How to pass a list of files to bash script with less code

Here is an example by using arrays:

NAMES=( "jeff" "david" "kenny" "randy" ) 

for NAME in ${NAMES[@]}; do
# Do something with NAME
echo "${NAME}"
done

And here https://linuxize.com/post/bash-arrays/#:~:text=Bash%20supports%20one-dimensional%20numerically,1%20references%20the%20last%20element the documentation.



Related Topics



Leave a reply



Submit