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

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.

Related

bash: finding file without keyword [duplicate]

This question already has answers here:
Grep : get all file that doesn't have a line that matches [closed]
(3 answers)
Closed 4 years ago.
I am looking for a command in bash that lists the files in which a keyword is not present. for listing files with the keyword I do
fgrep KEYWORD .
I was thinking I could feed vimdiff with two files with the lists, something like this
diff `fgrep KEYWORD .` `ls .` (THIS IS NOT CORRECT)
but I would not like to create two new files at hoc.
How about using simple grep option.
grep -L "foo" *
You could use --files-without-match option too with it.

How to make a vim variable determinable by bash? [duplicate]

This question already has answers here:
How to check if a files exists in a specific directory in a bash script?
(3 answers)
Closed 4 years ago.
I'm not sure how to word my question exactly...
I have the code
if grep "mynamefolder" /vol/Homefs/
then
echo "yup"
else
echo "nope"
fi
which gives me the output
grep: /vol/Homefs/: Is a directory
nope
The sh file containing the code and the directory I'm targeting are not in the same directory (if that makes sense).
I want to find the words myfoldername inside /vol/Homefs/ without going through any subdirectories. Doing grep -d skip, which I hoped would "skip" subdirectories and focus only directories, just gives me nope even though the folder/file/word I'm testing it on does exist.
Edit: I forgot to mention that I would also like mynamefolder to be a variable that I can write in putty, something like
./file spaing and spaing being the replacement for myfoldername.
I'm not sure if I did good enough explaining, let me know!
You just want
if [ -e /vol/Homefs/"$1" ]; then
echo yup
else
echo nope
fi
The [ command, with the -e operator, tests if the named file entry exists.
vim is not involved, and grep is not needed.
If you're insisting on using grep, you should know grep doesn't work on directories. You can convert the directory listing to a string.
echo /vol/Homefs/* | grep mynamefolder

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.

Cut sub-string and rename files use shell command [duplicate]

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

rename filenames under a folder, make them shorter [duplicate]

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

Resources