Master Linux File Renaming with mv and rename: Simple and Complex Techniques
This guide explains how to rename files on Linux using the mv and rename commands, covering basic single‑file renaming, batch renaming with patterns, handling different OS syntaxes, and advanced strategies for generating name mappings from custom files or filename patterns.
Linux file renaming can be performed with two core commands: mv for single‑file moves and rename for batch operations based on pattern substitution.
Simple renaming
mv old_name.txt new_name.txtdirectly renames a single file. rename supports batch renaming; for example, rename 'YSX' 'ehbio' *.gz replaces the string YSX with ehbio in all matching files.
Note that rename syntax differs between distributions: CentOS typically uses rename old new file_list, while Ubuntu uses the Perl‑style rename s/old/new/ file_list. Use man rename to verify the version.
Complex renaming with a mapping file
If you have a custom mapping file (e.g., name.map.txt containing "a Control" and "b Treatment"), you can generate rename commands with awk:
awk '{print "mv ehbio_"$1"_1.fastq.gz ehbio_"$2"_1.fastq.gz"; print "mv ehbio_"$1"_2.fastq.gz ehbio_"$2"_2.fastq.gz";}' name.map.txtCopy the output to a script or pipe it directly to bash. Alternatively, replace print with system inside awk to execute the moves immediately.
Generating mappings from existing filenames
When no explicit mapping exists, you can infer relationships using shell utilities. For a set of FASTQ files like A1_FRAS..._1.fq.gz, you can extract parts with cut and sed, then combine original and new names with paste:
paste <(ls A*.gz) <(ls A*.gz | cut -f 1,3 -d '_' | sed 's/\([A-E]\)/\1_/')Transform the paired list into mv commands (or ln -s for symlinks) by prefixing each line with the appropriate command.
Additional tips
Always quote patterns and be careful with characters that may match unintended parts of filenames.
When using rename, adding delimiters (e.g., underscores) can reduce accidental matches: rename '_a_' '_Control_' *.fastq.gz.
For Windows users, similar operations can be scripted with Git for Windows or PowerShell equivalents.
The examples above demonstrate a reproducible workflow: create or infer a mapping, generate concrete rename commands, review them, and execute safely.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
