Unix 101: Manipulating files – Copying, moving, deleting

This post is the next in a series of posts we’re doing on Unix. The aim of the series is to get our readers familiar with the basics of working on a Unix or a Linux system.

Uptil now we’ve learned a bit about how the Unix system works and how to navigate around the filesystem. We can now login to a unix system and start viewing the files and moving around between different directories. We also know a little about processes on a Unix system.

What next ?

This post will teach you about different file manipulation operations. By file manipulation, I mean, commands to copy, move or delete files. Since, on a Unix/Linux system everything is a file, the commands that we learn about today can also be used to work with directories, with some changes.

Unix commands have a reputation for being terse and short, which goes back to the old days when all input to a system was through some complex circuit manipulations or the computer had to be fed punch cards to recognize input. It made sense to use short commands for the purpose back then and modern Unixes/Linuxes have just carried on the tradition.

Take for example, the command to copy a file, cp, the syntax for which is:

# cp <sourcefile> <destinationfile>

For eg:

# cp test1 test2

The above command will just make a copy named test2 of the file named test1. Simple, isn’t it ?

Continuing the tradition of terse commands is the command to move files, mv.

# mv <sourcefile> <destinationfile>

For eg:

# mv test1 testdirectory/test2

The above command will copy the file named test1 to the directory named testdirectory and will change the name of the file to test2. If you don’t give the name of the destination file (test2 in the example), the file will be moved with the same name as the source.

The last command that we’ll be talking about today is the remove command, or rm. rm is the command to use when you want to remove a file from the system.

# rm <filename>

For example,

# rm test1

The above command will delete the file named test1. Use the command with care, though, since unlike Windows there is no concept of a recycle bin in Unix/Linux and any file that you delete using rm will be gone forever and you cannot change your mind later.

The above commands also work on directories, since a directory is also a file in Unix, a special file.

That was a basic overview of the cp, mv and rm commands. As always, I’d urge you to RTFM (read the fine manual – man pages in this case) and try out these commands for yourself. The only way to understand Unix is to use it.

I really hope this series of posts is benefitting atleast someone out there. I would love to hear some feedback from you guys on how I can improve my next posts and also what would you like me to cover.

Leave a Reply

Your email address will not be published. Required fields are marked *