Removing Part of a Filename for Multiple Files on Linux

Removing part of a filename for multiple files on Linux

First of all use 'sed -e' instead of '\e'

And I would suggest you do it this way in bash

for filename in *.fasta; do 
[ -f "$filename" ] || continue
mv "$filename" "${filename//test.extra/}"

done

Remove part of name of multiple files in Linux

Use rename.

rename 's/S\d{3}_//' *.fastq.gz

How to delete part of a filename using bash script

This should work:

for old in FOO*.dat.txt
do
new=$(echo $old | sed 's/.dat.txt/.txt/g')
mv "$old" "$new"
done

Remove part of file name in multiple sub directories

To make sure you do not accidentally modify other files or directories, you should make sure your script restrict itself to ONLY files staring with the same digits as the directory name or directories starting with the number and an underscore, and ONLY one layer deep.

Try this:

#!/bin/bash

declare base_dir=/path/to/Main

cd ${base_dir}
while read subdir; do
number=${subdir#./}
for file in $(find ${subdir} -maxdepth 1 -type f -name "${number}*"); do
mv ${file} ${subdir}/${file##./${number}/${number}}
done
for file in $(find ${subdir} -maxdepth 1 -type d -name "${number}_*"); do
mv ${file} ${subdir}/${file##./${number}/${number}_}
done
done < <(find . -maxdepth 1 -type d -regex './[0-9]*')

bash removing part of a file name

rename is part of the perl package. It renames files according to perl-style regular expressions. To remove the dates from your file names:

rename 's/[0-9]{14}//' CombinedReports_LLL-*.csv

If rename is not available, sed+shell can be used:

for fname in Combined*.csv ; do mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" ; done

The above loops over each of your files. For each file, it performs a mv command: mv "$fname" "$(echo "$fname" | sed -r 's/[0-9]{14}//')" where, in this case, sed is able to use the same regular expression as the rename command above. s/[0-9]{14}// tells sed to look for 14 digits in a row and replace them with an empty string.

Linux Bash - How to remove part of the filename of a file contained in folder

Short and simple, if you have GNU find:

find . -name '* - *.*' -execdir bash -c '
for file; do
ext=${file##*.}
mv -- "$file" "${file%% - *}.${ext}"
done
' _ {} +

  • -execdir executes the given command within the directory where each set of files are found, so one doesn't need to worry about directory names.
  • for file; do is a shorter way to write for file in "$@"; do.
  • ${file##*.} expands to the contents of $file, with everything up to and including the last . removed (thus, it expands to the file's extension).
  • "${varname%% - *}" expands to the contents of the variable varname, with everything after <space><dash><space> removed from the end.
  • In the idiom -exec bash -c '...' _ {} + (as with -execdir), the script passed to bash -c is run with _ as $0, and all files found by find in the subsequent positions.

Remove prefix from a LIST of files in bash

If you have the rename utility from the util-linux package available, it makes this kind of task very easy.

From its man :

  rename [options] expression replacement file...

rename will rename the specified files by replacing the first occurrence of expression in their name by replacement.

So in your case :

rename ave. '' ave.*


Related Topics



Leave a reply



Submit