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 writefor 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 variablevarname
, with everything after<space><dash><space>
removed from the end.- In the idiom
-exec bash -c '...' _ {} +
(as with-execdir
), the script passed tobash -c
is run with_
as$0
, and all files found byfind
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
Find All Files with Name Containing String
How Find Out Which Process Is Using a File in Linux
How Have Both Local and Remote Variable Inside an Ssh Command
Changing Environment Variable of a Running Process
Execute Combine Multiple Linux Commands in One Line
Bash - How to Pipe Result from the Which Command to Cd
Difference Between Arm-Eabi Arm-Gnueabi and Gnueabi-Hf Compilers
Bluetooth Le Signal Strength Linux
What Is the Correct Way to Start a Mongod Service on Linux/Os X
Difference Between Shell and Environment Variables
How to Pass a Wildcard Parameter to a Bash File
Append Date to Filename in Linux
Maximum Number of Bash Arguments != Max Num Cp Arguments
Sort Not Sorting as Expected (Space and Locale)
How to Read the Source Code of Shell Commands
Finding Docker Container Processes? (From Host Point of View)