Shell script looping directory [duplicate] - shell

This question already has answers here:
How to loop through file names returned by find?
(17 answers)
Looping through all files in a directory [duplicate]
(6 answers)
Closed 2 years ago.
I want to write a shell script that will loop through all the files in a directory and do echo "put ${filename}".
How to use a while loop for this logic.

You can get the list of all the files in a directory by using the find command and convert it in an array using the round brackets. Finally loop through the array and print it.
path=some_path
files=( $(find $path -maxdepth 1 -type f) )
for file in "${files[#]}"; do
do
echo "put $file"
done

Related

Can bash be used to rename by pattern? [duplicate]

This question already has answers here:
Rename files using regular expression in linux
(10 answers)
Closed 2 years ago.
I have files:
alpha_123_abc_file.txt
beta_456_def_file.txt
gamma_789_ghi_file.txt
Is there a way to rename all to cut the parts after the first _ character? To become:
123_abc_file.txt
456_def_file.txt
789_ghi_file.txt
I've looked into the perl tool but I an unsure if it has the capability to search out a pattern like that.
for file in *_*; do echo mv -- "$file" "${file#*_}"; done
Remove the echo when you're done testing and ready to actually do the mv.

How to order by file size and delete if size is zero [duplicate]

This question already has answers here:
Linux delete file with size 0 [duplicate]
(8 answers)
How to sort 'find' results in bash by size
(2 answers)
How do I parse command line arguments in Bash?
(40 answers)
Closed 4 years ago.
How do I write a script that will list all files in a user-defined directory, as well as all sub-directories and order them by size? As well as give the user the option to delete any file that is a size of 0.
The script I have so far lists all files in current directory as well as sub-directories
for file in $( find . -type f ${dir} );
do
echo "$(basename "$file")"
done

Process all files with a given ending in a directory [duplicate]

This question already has answers here:
Loop through all the files with a specific extension
(7 answers)
Closed 6 years ago.
I am trying to find and process all files with a given ending (.txt in the example below) in a directory. My current example finds all files containing .txt anywhere in the file name (e.g. also files with the ending .txt*, e.g. .txt.xls).
DATADIR=$1
for DATA in `ls $DATADIR`; do
DATABASENAME=$(basename $DATA)
echo "Basename of file $DATABASENAME"
if [[ ${DATABASENAME} =~ .*txt ]];
then
DATAPATH="$DATADIR$DATABASENAME"
echo "File path $DATAPATH"
fi
done
If I understand right, that is the for loop you want:
for file in *.txt ; do

Bash for loop and glob expansion [duplicate]

This question already has answers here:
Looping on empty directory content in Bash [duplicate]
(2 answers)
Closed 7 years ago.
Consider the following bash code:
for f in /tmp/*.dat; do echo ${f}; done
when I run this and there is no *.dat file in /tmp the output is:
/tmp/*.dat
which is clearly not what I want. However, when there is such a file, it will print out the correct one
/tmp/foo.dat
How can I force the for loop to return 'nothing' when there is no such file in the directory. The find-command is not an option, sorry for that :/ I would like to have also a solution without testing, if *.dat is a file or not. Any solutions so far?
This should work:
shopt -s nullglob
...
From Bash Manual
nullglob
If set, Bash allows filename patterns which match no files to expand
to a null string, rather than themselves.

for loop on files that don't exist [duplicate]

This question already has answers here:
How to skip the for loop when there are no matching files?
(2 answers)
Closed 3 years ago.
I want to process a set of files (*.ui) in the current directory. The following script works as expected if some *.ui files are found. But if no .ui file exist the current directory, the for loop is entered all the same. Why is that ?
for f in *.ui
do
echo "Processing $f..."
done
It prints :
Processing *.ui...
Use:
shopt -s nullglob
From man bash:
nullglob
If set, bash allows patterns which match no files (see Pathname Expansion
above) to expand to a null string, rather than themselves.
You already have the how, the 'why' is that bash will first try to match *.ui to files, but if that doesn't work (it gets no results) it will assume you meant the string "*.ui".
for f in "*.ui"
do
echo "Processing $f..."
done
wil indeed print "Processing *.ui".

Resources