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

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

Related

Shell script looping directory [duplicate]

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

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.

Shell Script- Traversing sub directories using shell scripts [duplicate]

This question already has answers here:
Bash: how to traverse directory structure and execute commands?
(6 answers)
Closed 4 years ago.
I am trying to traverse through the sub directories under current directory. There are certain files that i want to access and process inside each sub--directories. Can anyone help how can I access files inside sub directories?
"
for dir in /home/ayushi/perfios/fraud_stmt/*;
do echo $dir;
done;
"
This above script will echo all the sub directories. but instead of echoing I want to go inside the directories and access files that are present inside it.
find /home/ayushi/perfios/fraud_stmt/ -type f | while read fname; do
: do something on $fname here
done
This will search for all files (i.e. not actual directories) from the specified directory downwards. Note that you should enclose "$fname" in double quotes, in case it contains spaces or other "odd" characters.
An example using a recursive function
process_file() {
echo "$1"
}
rec_traverse() {
local file_or_dir
for file_or_dir in "$1"/*; do
[[ -d $file_or_dir ]] && rec_traverse "$file_or_dir"
[[ -f $file_or_dir ]] && process_file "$file_or_dir"
done
}
rec_traverse /home/ayushi/perfios/fraud_stmt
process_file can be changed to do something on file.
"$1"/* may be changed to "$1"/* "$1"/.* to match hidden directories but in this case special hard linked directories . and .. must be filtered to avoid infinite loop.

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

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