Rename multiple files

Ubuntu - Command Line Tools

Change file extensions

rename oldFileextension newFileextension FilesToRename

Examples

rename all .csv files into .txt files (focus only on *.csv files)

rename .csv .txt *.csv

rename .fasta files into .fna files

change all sampleID.fasta into sampleID.fna

rename .fasta .fna *.fasta

add species name (e.g., E.coli) to all .bam files

change all sampleID.bam files into sampleID_ecoli.bam

rename .bam _ecoli.bam *.bam

Ubuntu

error: syntax error at (eval 1) line 1, near "."

error: syntax error at (user-supplied code), near "."

getting these errors, means you have another rename command (Ubuntu) which uses Perl regular expressions:

Ubuntu: rename all .csv files into .txt files (same as above, but using the "Perl" based rename)

rename 's/\.csv$/.txt/' *.csv

general: renaming a string at any position in filename (Example: change all 'dataNew' into 'data2015')

rename 's/dataNew/data2022/' *.csv

Add / remove prefix

Perl based rename version - Ubuntu

change prefix of all textfiles

rename 's/^oldPrefix/newPrefic/' *.txt

# add prefix "projectABC_" to all .txt files

rename 's/^/projectABC_/' *.txt

# remove prefix "projectABC_" from all .txt files

rename 's/^projectABC_//' *.txt

Use shell loop if Ubuntu "rename" does not work (non-Perl rename command)

# add prefix "projectABC_" to all .txt files

for file in *.txt ; do mv "$file" "projectABC_$file" ; done

# remove prefix "projectABC_" from all .txt files

for file in projectABC_*.txt; do mv "$file" "${file#projectABC_}"; done

Add file extension to set of file

# add suffix ".txt" to all files in a folder

for FILE in *; do mv ${FILE} ${FILE}.txt; done