Rename file in Linux removing some strings [duplicate] - linux

This question already has answers here:
Rename multiple files based on pattern in Unix
(24 answers)
Closed 8 months ago.
I have a lot of files with this format:
download_agrupocqa_127_1656097965.tar.gz
download_bjxkwris_127_1656097966.tar.gz
download_climpieza_127_1656097965.tar.gz
download_dhermagqu_127_1656097966.tar.gz
Do you know some command to rename all files in a folder to this format?
agrupocqa.tar.gz
bjxkwris.tar.gz
climpieza.tar.gz
dhermagqu.tar.gz
Which means remove: download_ and _127_1656097965

In BASH:
for file in *.tar.gz ; do
suffix="${file#download_}" # this removes download_
prefix="${suffix%%_*.tar.gz}" # this removes everything after underscore
mv "$file" "${prefix}.tar.gz" # this renames the file
done
If you have sed:
for file in *.tar.gz ; do
newname=$(echo "$file" | sed 's/download_\([^_]*\)_[0-9_]*\..*/\1.tar.gz/')
mv "$file" "$newname"
done

Related

Rename a portion of a filename recursively throughout directory and sub directory [duplicate]

This question already has answers here:
Rename files and directories recursively under ubuntu /bash
(8 answers)
Closed 1 year ago.
How can I make this command also execute throughout sub directories?
for filename in *foo*; do mv "$filename" "${filename//foo/bar}"; done
Probably you want to rename only the filename (last pathname component), not a inbetween subdirectory name. Then this task can be accomplished using globstar feature of bash.
#!/bin/bash
shopt -s globstar
for pathname in ./**/*foo*; do
[[ -f $pathname ]] || continue
basename=${pathname##*/}
mv "$pathname" "${pathname%/*}/${basename//foo/bar}"
done

I need a script to replace old libraries with newer library in all files [duplicate]

This question already has answers here:
How to replace a string in multiple files in linux command line
(28 answers)
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Looping through the content of a file in Bash
(16 answers)
How to loop over files in directory and change path and add suffix to filename
(6 answers)
Closed 3 years ago.
I have numerous files in a directory and want to replace one of the libraries used in all of these files with a newer library because the old library no longer works.
I have used ls > names.txt to list all filenames into a txt document. I then attempted to write a bash script which would loop through all files, catch the old library, and replace it with the new library.
for entry in names.txt
do
sed 's/<libOld>/<libNew>/g' $entry > $entry
done
I expect the loop to go through each file name, find the old library used, and replace it with the new one. Running this script however doesn't appear to do anything.
You're bumping into a few common issues; I've closed as a duplicate, but here are the collected fixes for your specific case:
Editing a file in-place with sed
With GNU sed:
sed -i 's/<libOld>/<libNew>/g' filename
with any sed:
sed 's/<libOld>/<libNew>/g' filename > filename.tmp && mv filename.tmp filename
Looping over a file line by line
for entry in names.txt only ever sets entry to names.txt, it doesn't read its contents. This is also BashFAQ/001.
while IFS= read -r entry; do
printf '%s\n' "$entry"
done < names.txt
Looping over all files in a directory
You don't need a separate file, and you shouldn't use ls but globs:
for fname in ./*; do
printf '%s\n' "$fname"
done
Combined for your case
Notice the double quotes around $entry.
for entry in ./*; do
sed -i 's/<libOld>/<libNew>/g' "$entry"
done
which can be simplified to no loop at all:
sed -i 's/<libOld>/<libNew>/g' ./*

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

For loop in bash [duplicate]

This question already has answers here:
How to loop through a directory recursively to delete files with certain extensions
(16 answers)
BASH: Writing a Script to Recursively Travel a Directory of N Levels
(2 answers)
Closed 4 years ago.
I'm trying to write a bash script that recursively goes through files in a directory, writing the file's name and hexdump to a file. My current script:
#/bin/sh
touch hexdump.txt
for filename in logical/*; do
echo "$filename"
"$filename" >> hexdump.txt
hd /logical/"$filename" >> hexdump.txt
done
The current output is:
logical/*
./hexadecimalExtraction.sh: line 5: logical/*: No such file or directory
hd: /logical/logical/*: No such file or directory
How do i get it to interpret "logical/*" as the list of files within "logical" directory and not the filename itself???
"$filename" >> hexdump.txt
should probably be removed
Otherwise you are trying to run the filename itself.
Also you are looking for files in logical subdirectory in the current directory, but the trying to look in /logical/
You can't recurse with for filename in logical/*. In order to recurse, you have to use find.
To make find visit only files, not directories, use find -type f.
I don't know hd, but you probably want
find tutorials -type f | while read i; do
echo $i >> hexdump.txt
hd $i >> hexdump.txt
done
You're looking for the ** glob operator.
shopt -s globstar nullglob
for filename in logical/**/*; do
echo "$filename"
hd "$filename"
done >> hexdump.txt
filename will contain the full name of the matched files, which already includes the directory logical and any sub directories.

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