There's more to copying and renaming files on Linux than cp and mv. Try some commands and strategies that might surprise you and save you some time. Credit: Nana B Agyei Linux users have for many decades been using simple cp and mv commands to copy and rename files. These commands are some of the first that most of us learned and are used every day by possibly millions of people. But there are other techniques, handy variations, and another command for renaming files that offers some unique options. First, let’s think about why might you want to copy a file. You might need the same file in another location or you might want a copy because you’re going to edit the file and want to be sure you have a handy backup just in case you need to revert to the original file. The obvious way to do that is to use a command like “cp myfile myfile-orig”. If you want to copy a large number of files, however, that strategy might get old real fast. Better alternatives are to: Use tar to create an archive of all of the files you want to back up before you start editing them. Use a for loop to make the backup copies easier. The tar option is very straightforward. For all files in the current directory, you’d use a command like: $ tar cf myfiles.tar * For a group of files that you can identify with a pattern, you’d use a command like this: $ tar cf myfiles.tar *.txt In each case, you end up with a myfiles.tar file that contains all the files in the directory or all files with the .txt extension. An easy loop would allow you to make backup copies with modified names: $ for file in * > do > cp $file $file-orig > done When you’re backing up a single file and that file just happens to have a long name, you can rely on using the tab command to use filename completion (hit the tab key after entering enough letters to uniquely identify the file) and use syntax like this to append “-orig” to the copy. $ cp file-with-a-very-long-name{,-orig} You then have a file-with-a-very-long-name and a file-with-a-very-long-name file-with-a-very-long-name-orig. Renaming files on Linux The traditional way to rename a file is to use the mv command. This command will move a file to a different directory, change its name and leave it in place, or do both. $ mv myfile /tmp $ mv myfile notmyfile $ mv myfile /tmp/notmyfile But we now also have the rename command to do some serious renaming for us. The trick to using the rename command is to get used to its syntax, but if you know some perl, you might not find it tricky at all. Here’s a very useful example. Say you wanted to rename the files in a directory to replace all of the uppercase letters with lowercase ones. In general, you don’t find a lot of file with capital letters on Unix or Linux systems, but you could. Here’s an easy way to rename them without having to use the mv command for each one of them. The /A-Z/a-z/ specification tells the rename command to change any letters in the range A-Z to the corresponding letters in a-z. $ ls Agenda Group.JPG MyFile $ rename 'y/A-Z/a-z/' * $ ls agenda group.jpg myfile You can also use rename to remove file extensions. Maybe you’re tired of seeing text files with .txt extensions. Simply remove them — and in one command. $ ls agenda.txt notes.txt weekly.txt $ rename 's/.txt//' * $ ls agenda notes weekly Now let’s imagine you have a change of heart and want to put those extensions back. No problem. Just change the command. The trick is understanding that the “s” before the first slash means “substitute”. What’s in between the first two slashes is what we want to change, and what’s in between the second and third slashes is what we want to change it to. So, $ represents the end of the filename, and we’re changing it to “.txt”. $ ls agenda notes weekly $ rename 's/$/.txt/' * $ ls agenda.txt notes.txt weekly.txt You can change other parts of filenames, as well. Keep the s/old/new/ rule in mind. $ ls draft-minutes-2018-03 draft-minutes-2018-04 draft-minutes-2018-05 $ rename 's/draft/approved/' *minutes* $ ls approved-minutes-2018-03 approved-minutes-2018-04 approved-minutes-2018-05 Note in the examples above that when we use an s as in “s/old/new/”, we are substituting one part of the name with another. When we use y, we are transliterating (substituting characters from one range to another). Wrap-up There are a lot of options for copying and renaming files. I hope some of them will make your time on the command line more enjoyable. Related content how-to How to examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe