There is much more to copying and renaming files on Linux than cp and mv. Try out some commands and strategies to save your time.
For this purpose, users have been using cp and mv for decades. These were some of the few commands we learned and use every day, similarly, there are other techniques, handy variations, and other commands used for this purpose that provide unique options.
Copying Files in Linux – cp command
The need to copy a file? You may need it in another location or want to edit it and be sure you have a backup in case it needs to be reverted. To do this use commands like:
cp dekifile dekifile-new
Want to copy a large number of files? That strategy may get old fast.
Alternatives are:
- You can use tar to create an archive of all files you want to keep a backup of before editing them.
- You can use a loop to make backup copies easy.
The first option is quite straightforward. For all files in the current directory, use the command:
$tar cf yourdatafileexample.tar *
For a group of files that can be identified with a pattern, use the command:
tar cf myfiles.tar *.txt
In every case; you end up with myfiles.tar file which carries files in the directory or all files with .txt extension.
The easy loop allows users to make backup copies with modified names:
$ for file in *
< do
< cp $file $file-orig
< done
If you are backing up a file with a long name then the user can depend on using the tab command to use filename completion (press tab once you have entered enough letters to identify the file uniquely) and use syntax like this to append “-orig” to copy.
$ cp your-long-name-file-which-is-not-a-good-example{, -orig}
You then have file-with-a-very-long-name and a file-with-a-very-long-name file-with-a-very-longs-name-orig.
Renaming and Moving Files in Linux
Use the mv command for this purpose. This is used to move file towards a different directory, change the name and leave it in place, or in some do both.
$ mv myfile /tmp
$ mv myfile notmyfile
$ mv myfile /tmp/notmyfile
We can also use the rename command, trick to use it is to get used to its syntax however if you know some Perl, you may not find it tricky.
Let us see some examples if you wanted to rename some files in a directory to replace all upper case letters with lower ones. Generally, you don’t find such files with capital letters on Unix or Linux systems, but you can. Following is an easy way to do this without using the mv commands for each one. The /A-Z/a-z/ specification informs the command to change any letters that range from A-Z to a-z.
$ ls
DekiSoft-Linux-Tutorial.JPG MyFile
$ rename 'y/A-Z/a-z/' *
$ ls
DekiSoft-Linux-Tutorial.JPG MyFile
Use this to remove file extensions; you might get tired of seeing text files with the same .txt extensions. Just remove them- with one command:
What if you want to put the extensions back? Don’t worry you need to change the command for this. You need to understand the “s” before the first slash means “substitute”. What lies between the first two slashes is what needs to be changed, and what lies between the second and third slashes is what we want to change. $ represents the ends of the filename which we are changing to .txt.
$ ls
agenda.txt notes.txt weekly.txt
$ rename 's/.txt//' *
$ ls
agenda notes weekly
Other parts of filenames can be changed as well. For this, you need to keep the s/old/new rule in mind:
$ ls
old-file-2020 old-file-2019 old-file-2018
$ rename 's/draft/approved/' *file*
$ ls
Remember that in the examples explained above, when we use an s as in s/old/new, we are adding one part of the name with another. When we use y we are transliterating that is adding characters from one range to another. In Windows, you can use programs like Sensible File Renamer for renaming files or folders in batch.
MV command tips
Use mv command (man mv) for this purpose. This is similar to the other command except it moves it physically from one place to another despite being duplicated.
Common options available with this include:
- i – Interactive: This prompts users if the selected file overwrites an existing file in the destination directory. This is quite a good option as with this option in cp they shall be given a chance to make sure to want to replace an existing file.
- F – Force: This overrides the interactive mode and moves without prompting. Unless and unless the user knows what he is doing, such an option doesn’t play nice; thus be careful while using this.
- v – Verbose: This is used to show a list of all the files that are being moved.
Use the following command, if you wish to move a file out of your directory to another:
Mv dekisoft-shaheer.txt /home
Or
Mv dekisoft-shaheer /home/newuser/home/newuser/tigger by using absolute pathnames.
Last Words
So, this was the simplest guide on copying, renaming, and moving files in a Linux environment. I have used easy day-to-day words as examples to clear out the concept for you and showed the workings of all basic commands, including cp and mv.