How to Copy and Rename Files in Linux

How to Copy and Rename Files in Linux
Getting your Trinity Audio player ready...

How to Copy and Rename Files in Linux

In this article, we are going to see how to copy and rename files in linux with full details including traditional methods using mv and cp command-line tools in linux regardless of various distros.

Everyone who has already started or starting their foot in the linux world is well aware of the linux command line interpreter popularly known as CLI and does understand the importance of CLI.

What are Linux Commands?

Linux Commands is nothing but those are pre-compiled scripts which is always shipped in form of binaries and normally kept in /usr/bin.

In Linux, architecture /usr is referred to as users related folder. So files kept under /usr/bin which does not normally need any elevated privilege to run. Linux Commands found in /usr/bin in most cases are inquiry related command which does not allow to make any major changes in system configurations.

/usr/sbin is the folder in linux architecture is mainly containing linux commands which have the capabilities to make changes to the system and they normally need elevated privilege and those are popularly known as SUDO which means whatever superuser can do. /sbin folder has commands for system administrators who are authorized to make changes to the system based on fixing issues or any scheduled change as feature update to linux operating system.

How Many Linux Commands?

There is a huge number of linux commands. Considering the number of options associated with a single linux command number of linux commands becomes really too high. Summarizing all the linux commands is out of the scope in this share and also you can summarize in the full book, not in a single post.

For using linux effectively you need to familiarize yourself with all the linux commands.

For example please refer to 40 linux commands for beginners and also the Top 15 useful linux networking commands.

Le`s back to the original topic and let us see how to copy and rename files in linux.

How to Copy and Rename Files in Linux With mv command

mv command in linux is a short form of the move and is one of the essential linux commands which can be used for two basic essential requirements first one to move the location of the file in the file system and the other to change the name of the file also name as renaming.

If you are a new user or experienced user you must be familiar with how to access linux server. One of the very famous free tools is Putty.

You can Download PuTTy on your desktop and install it or you can simply have   PuTTY Portable and use it.

If you are interested to configure putty such as you will have all your sessions gets logged follow this procedure on How to Configure PuTTy.

So once you log in to the Linux Server and if interested to see help available for the mv command you can get it by typing the mv –-help command.

# mv --help
Syntax of mv command is like below
# mv [options] [SOURCE] [DESTINATION]
Where [SOURCE] – Source location of the file 
& [DESTINATION] – Destination location of the
file.
Below are few important options available with
the mv command.
-f = This is the force option omitting all mes
sages means no message displayed before overwr
iting a file.
-i = it is interactive mode warning messages t
o get displayed before overwriting a file.
-u = This uses validation like if the destinat
ion does not have a file move it else ignores
if already present. This can prevent over-writing.
only move a file if it is not present at the 
destination.
-v = This is verbose mode shows the mv command
activities cycle.

Copy and Rename Files on Linux Using the mv Command

If you want to rename a file, you can follow below syntax

# mv <file1> <file2>

In this scenario, file1 is going to be renamed with the name file2 and there will be no file1 anymore at the source location.

Above syntax, you can use in the present working directory otherwise if you have to move some file from one location to other location with renaming it you can use it like below.

# mv /var/log/messages /tmp/servername.messages

This mv linux command copy and rename files from /var/log to /tmp with a new name.

The full path you have to mention you might be already aware it is known as an absolute path. 

Copy and Rename Files in Linux Using the Rename Command

rename linux command will provide a bit more control. Most of the time it is delivered as standard package contents if not you can install it.

# yum –enablerepo=* install rename

Syntax of the rename command is like below.

$ rename  's/old-name/new-name/' files

It may sound complex and similar to pattern search once you become familiar it will be easy for you.

Let us show this practically by example. Here we will create a new folder called renamefile, and we will touch 5 files.

# mkdir renamefile
# cd renamefile
# touch file{1..5}.txt
# ls -l
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

If you want to rename a single file called file1.txt, the command will be like below.

# rename ‘s/file1/newfile1/’ file1.txt

If you want to change the extension to all files, for example, to .html.

# rename ‘s/.txt/.html/’ *.txt
# ls –l
file1.html
file2.html
file3.html
file4.html
file5.html

Just to let you know that rename uses a regular expression of Perl, meaning this command has endless possibilities.

Well, it is a good idea to have all the rename commands in linux. You can check using rename –help.

# rename -help
Some very common usage of rename commands is 
as below

Change filenames to uppercase
# rename 'y/a-z/A-Z/' *

Change filenames to lowercase
# rename 'y/A-Z/a-z/' *
Replace spaces with underscores
# rename 'y/ /_/' *

How to Remove rename Command in Linux

If you do not want to keep rename command you can easily remove using yum like below.

#yum –enablerepo=* erase rename

That’s it and rename command is removed from your Linux Machine.

Copy Files in Linux using the tar and cp Command

The tar command in linux is multi-dimensional. You can use it simply for copying purposes and the same can be used as long-time backup options and keeping it in form of tarball by zipping it and use it whenever needed and really helpful to save hard disk space to keep cost minimal. You just use a zipping tool like compress, gzip, or any other tool in linux.

With this quality, it is mostly used for copy and backup both even transferring files.

How to Create tarball in Linux

To create a tarball of all files in a directory you can simply use the below. This will create a tarball in the present working directory.

$ tar cvf abc.tar *

Next example we will use the absolute path. By using the absolute path you can create tarball wherever you want in the file system.

# tar cvf <Destination> <Source_Dir>
# tar cvf /var/tmp/etc.tar /etc  

To create a tarball of similar files that you can identify with a pattern, you’d use a command like this. To create a tarball of pdf files in your folder.

# tar cvf mypdf.tar *.pdf
# gzip mypdf.tar

This will convert your tarball to a .zip file.

So now you can say simply you have copied or backed up those files and you can copy them anywhere you want like below. We will see cp command options with tar and have better understandings.

Syntax of cp command like below

#cp options <source> <destination>

You can get more details about options using 
man.
# man cp
Copying entire contents from /abc directory to
/xyz directory you can do like this.
#cp –pr /abc /xyz

So let`s copy the tarball to a required location using the cp command if you have not used the absolute path during tarball creation.

#cp mypdf.tar.gz /xyz/
Now you can unzip it and extract it.
#cd /xyz
#gunzip mypdf.tar.gz

How to Extract tarball in Linux

# tar xvf mypdf.tar

This will place all files here and you are done. You can remove tarball from here.

So we have seen mv, rename, cp, and tar commands they are really helpful for copy and rename files in linux.

Conclusion

Copy and Rename files in Linux are basically very simple and most used tasks but sometimes very important to know in the interest of saving space or eliminating duplicates you have to judge what to use when. Knowing how to do it is something every server administrator must know.

The basic difference between cp, tar, and move is that mv does not leave source files while others do. So except mv others leaves duplicate copy. mv and rm commands in linux considered to be in the dangerous category due to not leaving the source instance so they have to be used carefully.

There are always various methods to achieve the same results as we have seen via our examples you need to decide which one to use in which condition.