Change file names iteratively in Linux [duplicate] - linux

This question already has answers here:
Rename multiple files in bash
(3 answers)
Closed 8 years ago.
The only way I am aware of to do this operation is with a for loop iterating over each file:
for file in *something.txt; do
out=\`echo $file | sed 's/something/else/'\`; mv $file $out;
done
I was wondering if there is any other way or shortcut for it (using GNU bash).

There is also simple substring replacement provided as part of bash itself:
mv $file ${file/something/else}
example:
$ touch {1..3}something.txt
$ ( for i in *something.txt; do mv $i ${i/something/else}; done )
$ ls -1 *else*
1else.txt
2else.txt
3else.txt

There's rename and the same basic loop concept as in your post only in whatever programming language you choose.

Related

Shell rename a lot of files recursively [duplicate]

This question already has answers here:
Find multiple files and rename them in Linux
(13 answers)
Closed 3 years ago.
I want to rename a lot of files in sub directories with a shell script/command, and I've tried different way without any success.
Here is the files I've got:
root/FOLDER1/media-125150-payasage151.jpg
root/FOLDER1/media-125165-payasage125.jpg
root/FOLDER2/media-1266165-payasage110.jpg
root/FOLDER2/media-1266165-portrait151.jpg
and I want to replace every "payasage" by "paysage"
root/FOLDER1/media-125150-paysage151.jpg
root/FOLDER1/media-125165-paysage125.jpg
root/FOLDER2/media-1266165-paysage110.jpg
root/FOLDER2/media-1266165-portrait151.jpg
I've tried RegExr with rename command or even with a mv approch...
thanks!
Try something along the lines of
for OLD in root/*/media-*-payasage*.jpg; do
NEW=$(echo "$OLD" | sed 's/payasage/paysage/g')
test "$OLD" != "$NEW" && mv "$OLD" "$NEW"
done

Writing a bash script to find all files in a directory that start with a, and do nothing if one exist [duplicate]

This question already has answers here:
Do not show results if directory is empty using Bash
(3 answers)
Closed 4 years ago.
So I have to find all the files in the directory that start with the letter a, and list them out. This is pretty easy by doing
cd some_directory
for file in a*; do
echo "$file"
done
However I want that if there are no files present that match a*, then the for loop will not run at all. Currently, if this is the case then the shell will echo
a*
Is there a way to do this? Thank you
Your text is opposite of your title, in my answer below I've assumed the text is your intention and your title is incorrect:
globs can be made to act like this with the bash shell option "nullglob":
shopt -s nullglob
An alternative is to use find and ignore errors by piping stderr to /dev/null
for file in $(find a* 2>/dev/null); do
echo "$file"
done

How to reverse a for loop in a bash script [duplicate]

This question already has answers here:
How to loop over files in natural order in Bash?
(7 answers)
Closed 5 years ago.
I need to reverse a for loop in a bash script. There is a directory with videos named after %Y%m.mp4 (201701.mp4; 201702.mp4, 201703.mp4, ...). The loop should start with the oldest filename (e.g. 201712.mp4) How to reverse my for loop?
outputdir="/path/to/video/monthly/"
for file in "$outputdir"*.mp4
do
echo $file
done
You can list your files in a reserved order in the following way:
ls -1 $outputdir/*.mp4 | sort -r
So in you script you can do:
outputdir="/path/to/video/monthly/"
for file in $(ls -1 $outputdir*.mp4 | sort -r)
do
echo $file
done
NOTE: As #PesaThe pointed out this solution would fail to work with filenames with spaces. If it is the case for you, you should quote the command: "$(ls -1 $outputdir*.mp4 | sort -r)" and use "$file"
UPDATE:
See the following test
mkdir test && cd $_
for i in {1..5}; do touch test_$i.txt; done
cd -
And ls -1 test/*.txt will output:
test/test_1.txt
test/test_2.txt
test/test_3.txt
test/test_4.txt
test/test_5.txt
And ls -1 test/*txt | sort -r will output:
test/test_5.txt
test/test_4.txt
test/test_3.txt
test/test_2.txt
test/test_1.txt

List paths with find when the filename contains spaces [duplicate]

This question already has answers here:
Iterate over a list of files with spaces
(12 answers)
Closed 9 years ago.
I have this code
for i in $(find pwd)
do
echo $i
done
the problem is if the file name contains spaces, it prints on a separate line
how can I list all of the files in some directory including files that contains spaces
This would have the intended effect of your example:
find /path/to/somewhere
That is, no need to wrap a for loop around it.
But I'm guessing you want something more than just echoing. Perhaps call a script for each file? You can do that with:
find /path/to/somewhere -exec path/to/script.sh {} \;
where {} will be replaced for each filename found.
I would use while read for this.
find . | while read i; do echo $i; done;
Edit:
Alternatively, you could just do ls -a1
here is the solution
IFS=$'\n'
for i in $(pwd)
do
echo $i
done

how to delete filename's specific suffix in a dir, in linux [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Extract filename and extension in bash
Linux: remove file extensions for multiple files
For example, A.txt B.txt, I want to rename then to A and B .
How can I do it with shell script? Or other method ? Thanks.
for i in *.txt; do mv "$i" "${i%.txt}"; done
I would use something like:
#!/bin/bash
for file in *.txt
do
echo "$file" "$( echo $file | sed -e 's/\.txt//' )"
done
Of course replace the two above references of ".txt" to whatever file extension you are removing, or, preferably just use $1 (the first passed argument to the script).
Michael G.
for FILE in *.txt ; do mv -i "$FILE" "$(basename "$FILE" .txt)" ; done

Resources