#!/bin/bash
cd $1
for f in *; do 
mv  "$f" "${f%.*}.bu"
done
Answer from Maythux on askubuntu.com
Discussions

Copy and rename file from shell script
It's hard to tell because the formatting is real messed up You can rename with just: cp /path/to/source/file /path/to/destination/newname More on reddit.com
🌐 r/bash
28
2
October 26, 2023
linux - Shell script to rename files - Stack Overflow
For rename.sh, the awk command returns nothing so in effect you have the following command the shell attempts to execute: ... you can fix this by testing for the filename of the script, either hardcoded or $0, and executing the mv command only if $FILE does equal the script name. More on stackoverflow.com
🌐 stackoverflow.com
Rename files on Ubuntu Terminal - Stack Overflow
I would like to rename my file C6Y5DACXX-2-ID01_1_sequence.fastq to C6Y5DACXX-2-ID01_sequence_1.fastq I am running a program that requires the input of the fastq files to be _1.fastq and _2.fastq More on stackoverflow.com
🌐 stackoverflow.com
bash - Rename all files in directory from $filename_h to $filename_half? - Stack Overflow
@Ryan @Ishan Jain In fact it indicates the backend shell that must execute the script. sh and bash are different shells so it may affect the script behavior and syntax. Since the script uses a bash built-in string replacement you should add the header #!/bin/bash. More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
freecodecamp.org › news › rename-file-linux-bash-command
Rename a File in Linux – Bash Terminal Command
September 30, 2022 - You can use the command below to rename all the files in the folder: for f in *.js; do mv -- "$f" "${f%.js}.html"; done · Let's break down this long string to see what's happening under the hood: The first part [for f in *.js] tells the for loop to process each “.js” file in the directory.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › bash shell › how to rename a file in bash
How To Rename A File In Bash - nixCraft
March 15, 2024 - We need to give SOURCE file to DESTINATION file using the following mv command syntax: $ mv oldname newname $ mv SOURCE DEST $ mv olddir newdir $ mv old-file new-file In short, the mv will rename SOURCE to DEST, or move SOURCE(s) (multiple files) ...
🌐
Linux Hint
linuxhint.com › rename_file_bash
How to Rename a File in Bash – Linux Hint
It is easy to rename a file by using a command in bash script. This article explains how to use the ‘mv’ and ‘rename’ bash commands to rename filenames.
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to rename files in linux
How to Rename Files in Linux (Multiple Options and Examples)
December 9, 2025 - The first line is the shebang, indicating a Bash script. The second line begins a for loop to iterate through files in the current directory ending with .txt. The third line uses the mv command on each file found to replace the .txt extension with .pdf.
🌐
Reddit
reddit.com › r/bash › copy and rename file from shell script
r/bash on Reddit: Copy and rename file from shell script
October 26, 2023 -

This is a pretty basic question but I’ve been struggling getting this working for some reason. I am trying to copy a file from one directory to another and renaming it along with the copy. This is being done inside of a shell script, and I have a variable called $filename that stores the NEW file name. Here is the code snippet:

filename="IRcam_fpga_cksm_${checksum}_ver_${version}.pdb"
#filename="This_is_a_file.txt"
echo "filename: ${filename}"
cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/$filename

The output of the echo command on the console is:

.pdbname: IRcam_fpga_cksm_A415_ver_0x0081

  But the file that gets copied to the new directory does not have the correct name. When I use the version of $filename that is commented out, it works perfectly fine.

Top answer
1 of 5
3
It's hard to tell because the formatting is real messed up You can rename with just: cp /path/to/source/file /path/to/destination/newname
2 of 5
2
The issue you're encountering is because you're overwriting the filename variable after you first set it. When you assign a new value to the filename variable, you essentially lose the previous value, which is why the file is copied with the default This_is_a_file.txt name instead of the intended IRcamfpga_cksm${checksum}ver${version}.pdb. To fix this, make sure that you're not overwriting the filename variable in the script. Here's an updated version of your code: bash filename="IRcamfpga_cksm${checksum}ver${version}.pdb" # Uncomment and use the correct $filename variable without overwriting it echo "filename: ${filename}" # Copy the file and rename it cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/"${filename}" Explanation: Define the filename correctly: Ensure that the variable filename has the correct value before it's used. Avoid overwriting filename: The second assignment (filename="This_is_a_file.txt") overwrites the first one, which is incorrect. You should either remove or not assign it again. Ensure proper file path: Use "${filename}" to reference the variable safely in the cp command. If checksum and version are being dynamically generated, ensure those variables are correctly populated before being used to build the filename. Here's an example where checksum and version are assumed to be variables with values: bash checksum="A415" version="0x0081" filename="IRcamfpga_cksm${checksum}ver${version}.pdb" echo "filename: ${filename}" # Now copy the file with the correct name cp ./par/ircam_fpga/designer/impl1/*.pdb output/pl/"${filename}" You can try renamer. ai to get easy file renaming solution.
Find elsewhere
Top answer
1 of 1
1

If we can assume that your file names do not contain ; (this is a huge assumption, ; is perfectly valid in file names, so you need to be sure about this), then you can just read your input text file, split on ; and save the result in two variables, allowing you to rename:

while IFS=';' read old new ; do mv "$old" "$new" ; done < filenames.csv

Here, we set the input field separator (IFS) to a ;, then read the input file, split on ; and assign the old and new filenames to their respective variables.

Now, your input example also has spaces around the ;. This means that the file name isn't "this_is_a_file.ext" but "this_is_a_file.ext " with an extra space, and that file won't exist. So before running the command above, you need to remove those extra spaces. You can do this with sed, telling it to replace all cases of two or more spaces ( *, that is one space, then another space and a *) around a ; with just the ;:

$ sed 's/  *;  */;/g' filenames.csv 
this_is_a_file.ext;new_filename_1.ext
this_is_another_file.ext;new_filename_2.ext
this is a filename with spaces.ext;new_filename_3.ext

Combining the two commands gives the one you really want to run:

$ sed 's/  *;  */;/g' filenames.csv |  
   while IFS=';' read old new ; do 
     echo "mv \"$old\" \"$new\"" ; 
done 
mv "this_is_a_file.ext" "new_filename_1.ext"
mv "this_is_another_file.ext" "new_filename_2.ext"
mv "this is a filename with spaces.ext" "new_filename_3.ext"

Once you are sure that is the right output, remove the echo and run again to actually rename the files:

sed 's/  *;  */;/g' filenames.csv |  
   while IFS=';' read old new ; do 
     mv -- "$old" "$new" ; 
done 

The -- is there to protect against file names starting with -. See What does "--" mean in terminal commands?.

🌐
nixCraft
cyberciti.biz › nixcraft › tutorials › linux › linux rename multiple files at a shell prompt
Linux Rename Multiple Files At a Shell Prompt - nixCraft
February 26, 2025 - The syntax is: $ rename oldname newname *.files On most modern Linux distros rename command is known as rename.ul: $ rename.ul oldname newname *.files For example rename all *.bak file as *.txt, enter: $ rename .bak .txt *.bak Please note that ...
🌐
Linuxize
linuxize.com › home › linux commands › how to rename files and directories in linux
How to Rename Files and Directories in Linux | Linuxize
March 12, 2026 - Use -i to be prompted before overwriting. “rename: command not found” The Perl rename utility is not installed. Install it with sudo apt install rename on Ubuntu/Debian or sudo dnf install prename on Fedora/RHEL.
🌐
Opensource.com
opensource.com › article › 21 › 8 › rename-file-linux-terminal
Rename a file in the Linux terminal | Opensource.com
To rename a file in the terminal, move the file with mv from itself to itself with a new name.
🌐
Cherry Servers
cherryservers.com › home › blog › linux › how to rename files in linux using mv & rename commands
How to Rename Files in Linux Using mv & rename Commands | Cherry Servers
November 7, 2025 - To illustrate this, create a new shell script using your preferred command-line text editor. ... Then use the for loop script as shown. In this example, the loop examines all the files with the .php extension and modifies the extension to .html. #!/bin/bash for i in *.php; do mv -- "$i" "${i%.php}.html" done ... The for i in *.php; do line iterates through all files with a .php file extension.
🌐
How-To Geek
howtogeek.com › home › linux › how to use the rename command on linux
How to Use the rename Command on Linux
September 8, 2023 - The last part ends the loop after each file has been processed. Most definitely. It is the rename command. rename is not part of a standard Linux distribution, so you will need to install it. It also has a different name in different families of Linux, but they all work the same way. You'll just have to substitute the appropriate command name according to the Linux flavor you're using. in Ubuntu and Debian-derived distributions you install rename like this:
🌐
LinuxConfig
linuxconfig.org › home › how to rename multiple files on linux
Rename Multiple Files on Linux with mv, rename, mmv
September 22, 2025 - We can also change the file extension of every file. The following command will change all files with the .log extension to .txt. $ for i in *.log; do mv -- "$i" "${i%.log}.txt"; done · You can also use the find command, along with -exec option or xargs command to rename multiple files at once.