Simple Commands

There are also a number of simple commands that you can employ to do useful work on your Linux system like those which are shown in Table 9-1.

Although we list a range of options which you can use with these commands, you should learn how to refer to the UNIX manual pages for a definitive explanation (see the section called Help!).

Table 9-1. Simple Commands

CommandExplanation
cdchange directory
pwdprint working directory
lslist files
mvmove a file
cpcopy a file
rmdelete a file
mkdirmake a directory
rmdirdelete a directory

Change Directory

The cd command allows you to change directory. To change to the /home directory, you would therefore type cd and /home at the command line.

cd /home

Print the Working Directory

You can also find out the location of the current directory as an absolute path from root by using the pwd command to display the result on screen. Our example consequently shows that we are currently in the /home directory.

pwd
/home

List Files

The list files command is invoked by typing ls in combination with a range of arguments which are shown in Table 9-2.

Table 9-2. The List Command

ls Command with OptionsExplanation
ls -alist all files
ls -llong-format (detailed) listing
ls --colorlist filetypes in colour
ls -Fshow filetype
ls -iproduce a detailed listing
ls -sprint file size

Tip: In order to take advantage of the listing filetype by colour facility which ls offers, you may need to establish an alias. Assuming that you are using Bash, enter the following line in your .bashrc file.

ls='ls --color'

Move Files

The mv command is used to move a file or directory from one place to another. The file sample.txt can therefore be moved into your /home directory by issuing the following command where the tilde (~) represents /home.

mv sample.txt ~

mv can also be used to rename a file. To change sample.txt's name to final.txt and move the renamed file to the /home directory, enter the following command:

mv sample.txt ~/final.txt

Copy a File

cp is very similar to mv in terms of the way in which you use it. One notable difference is that cp leaves the source file in situ whilst copying the file to your intended destination.

cp sample.txt ~/home

Tip: cp is also used to copy directories including their contents. If you need to do this, issue the cp command in conjunction with the -R option before typing the names of your source and destination directories.

Delete a File

The rm command is very easy to use although it is extremely easy to delete files unintentionally! We therefore advise you to use the -i option so that Linux confirms your intentions before taking any action.

rm -i sample.txt

Make a directory

You can use the mkdir command to create directories and sub-directories. Creation of a directory therefore involves typing the command and the name of your proposed directory whereas the creation of a sub-directory depends upon the -p option.

mkdir work

mkdir -p /work/paul

Delete a Directory

It's easy to delete an empty directory with the rmdir command. If we assume that the pleasure directory is empty, the next command removes it from our system.

rmdir pleasure

Tip: You should use the rm with the -R option to delete a directory which contains files.