Linux Command Line Basics Part III: Files & Directories

Knowing basic *nix terminal commands is an absolute must for any computer pro. Whether you use Windows, Mac OS or Linux, you’re bound to face the command prompt at some stage, so here’s my crash course in CLI. In Part 3, I’ll be covering creation, deletion and manipulation of files and directories.

File Operations


‘cp’ is Unix’s copy file command. It takes two arguments – source & destination. If no directory is specified, cp assumes that the files are contained in the working directory, however you can explicitly specify the source, destination or both without having to navigate to either location. In the example below, the user has copied the existing file ‘presentation.ppt’ to a new file ‘old-presentation.ppt’ in the folder ‘Archive’.

Copying 'presentation.ppt' to a new file in a different folder


‘mv’ is the ‘file move’ command in Unix. It operates in a similar manner to ‘cp’, where the same rules for source and destination apply, however ‘mv’ will actually move a file from its original location to a new path. In the example below, the user has moved ‘chart.jpg’ from the ‘Work’ folder to ‘Archive’. In this case, the file name has been preserved because no new name was specified in the destination, however by changing the destination command to ‘$ mv chart.jpg Archive/old-chart.jpg‘ the filename will be altered in the new copy.

This command can also be used to rename files – if you don’t set a destination, mv will use its default behaviour and assume that the target is within the working directory, effectively a rename function.

mv also works with directories, with no need for additional options.

Moving 'chart.jpg' to Archive


‘rm’ is used to remove (delete) files. In the example below, the file ‘old-spreadsheet.xls’ is deleted from the ‘Work’ folder. As with the commands above, the default action is to operate within the working directory unless another directory is specified explicitly.

When you’re working on the command line, there is no Recycle Bin or Trash as a failsafe, so be sure that you’re working with the right files before you start removing things.

If you want to remove all files with a particular extension, you can use the Unix wildcard – *. For example, if you wished to remove every file with the ‘.class’ extension (a very familiar scenario for Java programmers) then you could use the command ‘$ rm *.class‘.

However, it’s vitally important that you’re careful when removing files using the wildcard – there are no second chances here, and hitting enter by accident when you’ve typed ‘$ rm *‘ will empty a folder. Use with caution!

Removing the 'old-spreadsheet.xls' file using rm

Directory Operations


Use the ‘mkdir’ command when you need to create a new directory. The location can be specified, but the default behaviour is to create in the working directory. In the example below, the user has created a new folder called ‘Presentations’

Creating the 'Presentations' folder using mkdir


Using the ‘-r’ option allows you to use ‘cp’ and ‘rm’ on directories. (The ‘r’ stands for ‘recursive’). In the example below, the ‘Presentations’ folder is copied, using ‘cp -r’, into the ‘Archive’ directory. The user then proceeds to navigate to ‘Archive’ and remove the ‘Spreadsheets’ folder using ‘rm -r’.

There is a similar command to ‘rm -r’, ‘rmdir’, for removing directories, however this command only works with empty directories: ‘rm -r’ removes all contents as well.

Copying and removal of directories using the -r option

Bonus Command Line Tricks

Tab Complete

Now that we’re comfortable navigating around the file structure and creating, removing and manipulating files and directories, it’s probably becoming pretty time consuming to type out long file paths time and time again. Luckily, most shells include a time saving trick to help you out.

Tab Complete allows you to autocomplete a path by partially typing it and pressing the ‘tab’ key. If there are multiple possibilities, double-pressing tab presents you with the available options.

In the example below, there are multiple directories beginning with ‘Archive20*’, each one corresponding to a different year. Tab complete has automatically filled in as much as it can and presents the user with a list of possible options. All the user has to do is complete the path with a year of their choosing.

Tab complete in action

Command History

This is another very simple but very useful tip. Most Unix shells support command history – just like web history, this is a list of previously entered commands. To access this history, simply press the ‘up’ arrow key to view the most recent entry. You can use the up and down arrow keys to navigate through your history.

That covers the basics of creating, deleting and manipulating files & directories. The next section of the guide covers some common (and very useful) Unix command line tools.

For a full list of topics covered in this tutorial series, head over to the index page.

One Response to “Linux Command Line Basics Part III: Files & Directories”

  1. […] the next section of the guide, we’ll start creating and manipulating […]

Leave a Reply