Basic Commands

Basic File Handling Commands

Basic Navigation commands


"cd" The change directory. This command will navigate you from your current location to the path specified after the command.
"cd /" will take you to the root directory of your system.
"pwd" This command is issued to display your current working directory.

If you issued the above change directory command, your pwd command will now indicate you are at the root directory location.


john@john-desktop:~$ cd /
john@john-desktop:/$ pwd
/




Absolute Paths and Relative Paths


In the above example we passed the path / to the cd command. It tells cd where to go. The "/" argument is an absolute path meaning that it specifies a location relative to the root of the filesystem tree. There are many other absolute paths. One easy way to recognise an absolute path is that they all start with a "/". For example, If I navigate to the path /home/john, we would be telling the cd command to navigate to the "/"location then the home location and finally the john area.

As well as absolute paths, there are also relative paths. These paths are relative to the current directory. For example, if you are in the location /home and you wanted to navigate to the directory "john", you would only need to enter the command "cd john".


john@john-desktop:~$ cd /
john@john-desktop:/$ pwd
/
john@john-desktop:/$ cd home
john@john-desktop:/home$ pwd
/home
john@john-desktop:/home$ cd john
john@john-desktop:~$ pwd
/home/john
john@john-desktop:~$ cd /
john@john-desktop:/$ pwd
/
john@john-desktop:/$ cd /home/john
john@john-desktop:~$ pwd
/home/john


Relative directories normally contain one or more special ".." directories. These ".." directories point to the parent directory. In the following example, we pass ".." to the cd command to move back one level.
Another useful argument to pass to the cd command is the "-". This argument means go back to the previous location. Another useful short cut is the passing of the "~". This argument when passed to the cd command will take you to your home area. An even quicker way to get to your home are is to issue the "cd" command with no arguments. In my example this is /home/john. It is also possible to use the "~" to specify other peoples home directories. "cd ~family" would take me to the /home/family directory.


john@john-desktop:/home$ ls -al
total 24
drwxr-xr-x 4 root root 4096 Oct 22 2011 .
drwxr-xr-x 24 root root 4096 Dec 19 23:40 ..
drwxr-xr-x 31 family family 4096 Nov 14 17:20 family
drwxr-xr-x 70 john john 12288 Jan 16 20:09 john
john@john-desktop:/home$ pwd
/home
john@john-desktop:/home$ cd ..
john@john-desktop:/$ pwd
/
john@john-desktop:/$ cd -
/home
john@john-desktop:/home$ pwd
/home
john@john-desktop:/home$ cd john
john@john-desktop:~$ pwd
/home/john
john@john-desktop:~$ cd ..
john@john-desktop:/home$ pwd
/home
john@john-desktop:/home$ cd ~
john@john-desktop:~$ pwd
/home/john
john@john-desktop:~$ cd /
john@john-desktop:/$ pwd
/
john@john-desktop:/$ cd
john@john-desktop:~$ pwd
/home/john


The ".." directory is not the only special directory. You will notice in the above listing that there is also a "." directory. Although the "." is not used with "cd" command it is often used to specify the current location. Often the dot prefixes a program or script that you wish to run from the current location. We can then issue a command "./myscript". The "./" indicates that we are running from the current location the script called "myscript"


Listing files with the "ls" command


One of the most useful and common commands that you will encounter is the "ls" command. (Literally meaning list). When "ls" is run alone, it will list any normal files within the current directory. If you add the "-a" parameter to the "ls" command thus making "ls -a", any files including hidden files will be displayed in the current directory. In Linux, hidden files are file names that begin with a dot ".". If you supply the "-l" option to the "ls" command, then we get an extended or long view of the files in the current directory. It is also possible to pass a directory to be displayed to the "ls" command. "ls -l /home/john" this would display all files in the /home/john area. Another useful parameter that can be passwd to the "ls" command is the "-t" option. The "-t" option sorts by modification time (newest first). Passing the "-r" option will reverse the order. Multiple options can be added together "ls -rtla". This command lists all files including hidden in reverse time order with the extended listing option!


$ pwd
/home/landoflinux
$ ls
examples.desktop visiblefile
$ ls -a
. .bash_logout examples.desktop .profile
.. .bashrc .hiddenfile visiblefile
$ ls -l
total 16
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
-rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile
$ ls -la
total 40
drwxr-xr-x 2 landoflinux landoflinux 4096 Jan 17 20:19 .
drwxr-xr-x 5 root root 4096 Jan 17 20:15 ..
-rw-r--r-- 1 landoflinux landoflinux 220 Mar 31 2011 .bash_logout
-rw-r--r-- 1 landoflinux landoflinux 3486 Apr 3 2012 .bashrc
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
-rw-rw-r-- 1 landoflinux landoflinux 458 Jan 17 20:17 .hiddenfile
-rw-r--r-- 1 landoflinux landoflinux 675 Mar 31 2011 .profile
-rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile
$ ls -rtl
total 16
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
-rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile
$ ls -rtla
total 40
-rw-r--r-- 1 landoflinux landoflinux 675 Mar 31 2011 .profile
-rw-r--r-- 1 landoflinux landoflinux 220 Mar 31 2011 .bash_logout
-rw-r--r-- 1 landoflinux landoflinux 3486 Apr 3 2012 .bashrc
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxr-xr-x 5 root root 4096 Jan 17 20:15 ..
-rw-rw-r-- 1 landoflinux landoflinux 458 Jan 17 20:17 .hiddenfile
-rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile
drwxr-xr-x 2 landoflinux landoflinux 4096 Jan 17 20:19 .


Long - extended file listings "-l"


When you issue the "-l" option, much more detail is displayed about your files. The very first column displays the file type and permissions of that file within the listing. The next column indicates how many links there are to this file system object. The third and fourth column indicate the file owner and the group. (These details will be covered later in more detail). The fifth column indicates the size. The sixth column is the modification time/date. And finally the last column is the name of the file. If you see a "->" then this indicates there is a symbolic link to another file. (This will be covered in more detail later).


Display directory information "-d"


As we have seen earlier, if we pass a directory path to the "ls" command, the contents of that directory are displayed. However, sometimes it useful to display information about the directory without display its contents. To achieve this, we pass the "-d" flag". "ls -ld /home/john" would display information relating to the directory and not its contents:


$ cd /
$ pwd
/
$ ls -ld /home/john
drwxr-xr-x 70 john john 12288 Jan 17 19:52 /home/john


Recursive Listings "-R"


Sometimes you may wish to display directory information and also any other files and directories underneath. In the following example we use the "-ld" option to display directory information. Next we use the "-R" recursive listing option to display both directory and sub directory files and information:


$ pwd
/home/landoflinux
$ ls -ld test1
drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:47 test1
$ ls -ld test1/test2
drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:49 test1/test2
$ ls -ld test1/test2/test3
drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:50 test1/test2/test3
$ ls -lR test1
test1:
total 8
-rw-rw-r-- 1 landoflinux landoflinux 397 Jan 17 20:47 file1.txt
drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:49 test2

test1/test2:
total 8
-rw-rw-r-- 1 landoflinux landoflinux 134 Jan 17 20:49 file2.txt
drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:50 test3

test1/test2/test3:
total 8
-rw-rw-r-- 1 landoflinux landoflinux 134 Jan 17 20:50 file3.txt
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 17 20:46 test4

test1/test2/test3/test4:
total 0


Inodes - What are they?


Every file on a linux server when created is given a "inode" number. This is a unique index number. To display inode information, the "-i" parameter is passed to the "ls" command. Although the inode number is unique, it is possible to see more than one occurrence of the same inode number referring to a file or directory of a different name. When you come across this, you have found yourself a symbolic link. This can be where one directory is linked to another or a file is linked to file name. For example, if we create a file called "link1", this will be assigned an inode number. If we then create a link from file "link1" to "link2", both these will now show the same inode number. In the example below we created a file called link1, then issued a link command "ln link1 link2":


$ ls -li
total 24
4456702 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 17 20:35 1
4456697 -rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
4468756 -rw-rw-r-- 1 landoflinux landoflinux 396 Jan 17 20:36 file1
5512561 drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:47 test1
4456699 -rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile
$ touch link1
$ ls -li
total 24
4456702 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 17 20:35 1
4456697 -rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
4468756 -rw-rw-r-- 1 landoflinux landoflinux 396 Jan 17 20:36 file1
4456701 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 17 21:18 link1
5512561 drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:47 test1
4456699 -rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile
$ ln link1 link2
$ ls -li
total 24
4456702 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 17 20:35 1
4456697 -rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
4468756 -rw-rw-r-- 1 landoflinux landoflinux 396 Jan 17 20:36 file1
4456701 -rw-rw-r-- 2 landoflinux landoflinux 0 Jan 17 21:18 link1
4456701 -rw-rw-r-- 2 landoflinux landoflinux 0 Jan 17 21:18 link2
5512561 drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 20:47 test1
4456699 -rw-rw-r-- 1 landoflinux landoflinux 526 Jan 17 20:17 visiblefile


You should now see that link1 and link2 share the same inode number (This is the number in the first column). This is because link1 and link2 are really the same thing. If some data were to be appended to the file "link1", then this would also be displayed within the file "link2".


$ pwd
/home/landoflinux
$ ls -li li*
4456701 -rw-rw-r-- 2 landoflinux landoflinux 0 Jan 17 21:18 link1
4456701 -rw-rw-r-- 2 landoflinux landoflinux 0 Jan 17 21:18 link2
$ echo "Welcome to Linux" > link1
$ ls -li li*
4456701 -rw-rw-r-- 2 landoflinux landoflinux 17 Jan 17 21:26 link1
4456701 -rw-rw-r-- 2 landoflinux landoflinux 17 Jan 17 21:26 link2
$ cat link1
Welcome to Linux
$ cat link2
Welcome to Linux


Later on in our "Learning Linux Guide", we will go into more detail about creating symbolic links - Hard and Soft links.



Creating Directories - "mkdir"


The "mkdir" command is used to create new directories. Directories allow you to organise your files and keep your system tidy. You will notice while navigating around your linux system that there are literally hundreds if not thousands of directories. Important system files and users files are stored within. In most cases, we will be working within our home area /home/user. In the example that follows we will be using /home/landoflinux. Simply issuing the "mkdir" command followed by a name will create you a single directory: "mkdir mydir1". If we wanted to create a directory with sever sub-directories, then we can not just issue "mkdir one/two/three" because we will receive an error "mkdir: cannot create directory `one/two/three': No such file or directory". To create nested directories, we would have to pass a "-p" flag to our command. This option will then create any missing directories. So now our command would be "mkdir -p one/two/three"


$ pwd
/home/landoflinux
$ mkdir mydir1
$ ls -l
total 16
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 17 21:39 mydir1
$ mkdir one/two/three
mkdir: cannot create directory `one/two/three': No such file or directory
$ mkdir -p one/two/three
$ ls -lR one
one:
total 4
drwxrwxr-x 3 landoflinux landoflinux 4096 Jan 17 21:45 two

one/two:
total 4
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 17 21:45 three

one/two/three:
total 0


Copying and Renaming Files - "cp" and "mv"


In this section we will learn how to create a file, copy the file and then rename the file. Firstly, we need to look at how we can create a file. There are numerous ways of creating files, either by a text editor of your choice or from simple commands or programs. In the examples below we will look at using the "touch" command, the "echo" command and simple redirection commands to generate a file.

"touch " is a command that literally modifies the time stamp of a file. If the file didn't already exist, it is then created. The file will be created as an empty file. The command "touch emptyfile" will create a file called emptyfile with a size of 0 bytes and a time/date stamp of the current time.

"echo " is a command that generates output to the screen (standard out). The command can be used to display a message or display the contents of a variable. "echo" is widely used in interactive scripts. In our example we are going to issue the command echo "Output redirected to a file" > echofile. This command has now created a file called "echofile" containing the text "Output redirected to a file". The ">" arrow is a simple redirection command that sends output to a file. If the file already exists, then it is overwritten. If we didn't want to overwrite the file, we could use the append option ">>". This will not overwrite the file, simply append any output to the file if it exists. If the file didn't exist, it would be created. To view the content of a file we are using the common command "cat". Although its purpose is really to concatenate files together, it is very useful for displaying the contents of a single file


$ pwd
/home/landoflinux
$ touch emptyfile
$ ls -l
total 12
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 emptyfile
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
$ echo "Output redirected to a file" > echofile
$ ls -l
total 16
-rw-rw-r-- 1 landoflinux landoflinux 28 Jan 18 10:00 echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 emptyfile
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
$ cat echofile
Output redirected to a file
$ echo "Output appended to file" >> echofile
$ cat echofile
Output redirected to a file
Output appended to file


Now that we have created a couple of test files "echofile and emptyfile". We are now going to copy these files to another file using the "cp" command. Once we have copied the files, we are then going to rename the files using the "mv" move command (rename).
The syntax of the "cp" command in its simplest form is "cp file1 file2". Literally meaning copy file1 to file2.


$ ls -l
total 16
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 emptyfile
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
$ cp echofile echofile2
$ cp emptyfile emptyfile2
$ ls -l
total 20
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 echofile
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 emptyfile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
$ ls -il
total 20
4469052 -rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 echofile
4469053 -rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
4469051 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 emptyfile
4469054 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
4456697 -rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
$ mv echofile renamed_echofile
$ mv emptyfile renamed_emptyfile
$ ls -il
total 20
4469053 -rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
4469054 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
4456697 -rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
4469052 -rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 renamed_echofile
4469051 -rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 renamed_emptyfile


From the above example we can see that the files copied have their own unique inode number. However, the files that were renamed retained there original inode number.

As mentioned earlier, the "mv" command actually stands for "move". Another use of this command is to move files from one location to another. In the following example, we are going to create a new directory with a small file. We are then going to move that file to another location. When a file is moved, it leaves its current location and is placed in its target destination.


$ pwd
/home/landoflinux
$ mkdir newdir1
$ cd newdir1
$ ls -al > testfile1
$ ls -l
total 4
-rw-rw-r-- 1 landoflinux landoflinux 188 Jan 18 10:57 testfile1
$ cd ..
$ pwd
/home/landoflinux
$ ls -lR newdir1
newdir1:
total 4
-rw-rw-r-- 1 landoflinux landoflinux 188 Jan 18 10:57 testfile1
$ mkdir newdir2
$ ls -l
total 28
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 10:57 newdir1
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 10:59 newdir2
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 renamed_echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 renamed_emptyfile
$ pwd
/home/landoflinux
$ cd newdir1
$ ls
testfile1
$ mv testfile1 ../newdir2/
$ cd ../newdir2
$ ls -l
total 4
-rw-rw-r-- 1 landoflinux landoflinux 188 Jan 18 10:57 testfile1
$ pwd
/home/landoflinux/newdir2


Deleting Files and Directories - "rm" and "rmdir"


So far we have learnt some basic ways of creating files and directories. Now we need to learn how to remove these files and directory structures. The most common way of removing a file is to use the "rm" remove command. In its simplest form "rm file1" will remove the file specified. However, you can specify more than one file after the "rm" command. "rm file1 file2". It is also possible to use pattern matching "rm file*". In the pattern matching example, any files that start with the name file will be removed from within your current location. Although this is a very convenient way of removing files, it can also be very dangerous if you make a mistake. To verify that you want to delete a particular file or a group of files matching a certain pattern, we can use the -i option. This means prompt me before removal. It is also possible to create an alias of the "rm" command to substitute the "-i" parameter each time the command "rm" is encountered. The alias command would take the format alias rm="rm -i" . This alias entry can be added into your ~/.bashrc file using a text editor of your choice.


$ pwd;ls -l
/home/landoflinux
total 28
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir1
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir2
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 renamed_echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 renamed_emptyfile
$ touch deleteme.txt
$ ls -l
total 28
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 11:47 deleteme.txt
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir1
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir2
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 renamed_echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 renamed_emptyfile
$ rm deleteme.txt
$ ls -l
total 28
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:38 echofile2
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 10:39 emptyfile2
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir1
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir2
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 renamed_echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 renamed_emptyfile
$ rm echofile2 emptyfile2
$ ls -l
total 24
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir1
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir2
-rw-rw-r-- 1 landoflinux landoflinux 52 Jan 18 10:01 renamed_echofile
-rw-rw-r-- 1 landoflinux landoflinux 0 Jan 18 09:59 renamed_emptyfile
$ rm renamed*
$ ls -l
total 20
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir1
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 newdir2


Now there are two different ways to remove a directory. The most obvious command is "rmdir" which as you can guess stands for remove directory. As a safety precaution, if you issue the "rmdir" command against a directory that still has files in, then the command will fail with the following error: rmdir: failed to remove `testdir1': Directory not empty . To rectify this we can remove any files from under that directory and then reissue the "rmdir" command:


$ pwd
/home/landoflinux
$ ls -l
total 16
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 11:02 testdir1
$ ls -lR testdir1
testdir1:
total 4
-rw-rw-r-- 1 landoflinux landoflinux 188 Jan 18 10:57 testfile1
$ rmdir testdir1
rmdir: failed to remove `testdir1': Directory not empty
$ cd testdir1
$ rm testfile1
$ cd ..
$ pwd
/home/landoflinux
$ ls -l
total 16
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 12:09 testdir1
$ rmdir testdir1
$ ls -l
total 12
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop


The other, way to remove a directory and remove any files that may be contained in the directory is to use the "rm" command, however, this time we are going to pass the parameter "-fr". The command "rm -fr directory_name" will then remove any files found in the directory and then remove the directory. This particular approach is generally favoured by administrators. Please be very careful that you know what you are removing! Once issued, there is no return. In the example below, we will demonstrate the "rm -fr" command.


$ pwd
/home/landoflinux
$ ls -lR
.:
total 16
-rw-r--r-- 1 landoflinux landoflinux 8445 Apr 16 2012 examples.desktop
drwxrwxr-x 2 landoflinux landoflinux 4096 Jan 18 12:21 testdir1

./testdir1:
total 4
-rw-rw-r-- 1 landoflinux landoflinux 455 Jan 18 12:21 testfile1
$ rm -fri testdir1
rm: descend into directory `testdir1'? y
rm: remove regular file `testdir1/testfile1'? y
rm: remove directory `testdir1'? y


In the above example we issued "rm -fri testdir1". By adding the "i" option to the "-fr" we were then asked to confirm our actions step by step. Although this is very useful function, if you were deleting a directory with many other directories and files, this could take a while.