This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 9 years ago.
I have a folder contains many .ass files for subtitles of a TV show
but the file name is just too long:
House.of.Cards.S01E01.WEBRip.720p.H.264.AAC.2.0-HoC.SOME-OTHER-THINGS.ass
House.of.Cards.S01E02.WEBRip.720p.H.264.AAC.2.0-HoC.SOME-OTHER-THINGS.ass
House.of.Cards.S01E03.WEBRip.720p.H.264.AAC.2.0-HoC.SOME-OTHER-THINGS.ass
now I just want to change them into shorter form:
House.of.Cards.S01E01.ass
House.of.Cards.S01E02.ass
House.of.Cards.S01E03.ass
is there any simple way to do it? (may be via shell script)
Thanks in advance
there is a (not so) simple way to do it
for f in *; do mv "$f" "${f//.H.*/}"; done
for f in *; do mv "$f" "$f.ass"; done
Related
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
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.
This question already has answers here:
How to loop through file names returned by find?
(17 answers)
Closed 3 years ago.
I am dealing with a legacy codebase where we're trying to convert all jpeg/png files to webp format using the cwebp command. Unfortunately, a lot of the image files were saved with spaces in the name.
Example: i am poorly named.jpg
So when running the following bash script to find all jpegs in the directory and loop through and convert them the words separated by spaces are treated as another file so the image never gets converted.
We don't want to remove the whitespaces, but just create a webp file with the exact same name.
files=$(find ./ -type f -name "*.jpg")
for jpg in $files
do
webp="${jpg/%jpg/webp}";
if [ ! -f $webp ]; then
echo "The webp version does not exist";
cwebp -q 80 "$jpg" -o "$webp";
fi
done
I've tried placing jpg=$(printf '%q' "$jpg") immediately after the do in the above code as well as other things.
I expect i am poorly named.webp to be created if file i am poorly named.jpg exists.
But there is no real reason to store all filenames. So an alternative is:
find ./ -type f -name "*.jpg" | while read jpg
do
....
But this works only, if a filename contains no linefeed. For files with linesfeeds there are other solutions. Ask for it if needed.
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.
This question already has answers here:
How to rename with prefix/suffix?
(10 answers)
Closed 7 years ago.
How to rename file names? I want to map names:
abc-hdpi.png ⟶ abc.png
bcd-hdpi.png ⟶ bcd.png
...
I have many files to do this, so mv abc-hdpi.png abc.png is not a good solution.
Search for prename (Perl rename) command; it can do the job easily:
prename 's/-hdpi.png/.png/' *-hdpi.png
Failing that:
for file in *-hdpi.png
do
mv "$file" "${file%-hdpi.png}.png"
done
You can extract from a fixed length string like so:
NAME[0]="abc-hdpi.png"
NAME[1]="def-hdpi.png"
NAME[2]="ghi-hdpi.png"
NAME[3]="jkl-hdpi.png"
NAME[4]="mno-hdpi.png"
rename(){
var=$1
mv $var $var{0:2}$var{8:}
}
for i in "${Name[#]}"
do
rename $i
done