Menu
Basic Linux File and Directory Commands

For this article I'm using Linux Ubuntu 12.04 LTS, but I doubt that it makes much difference which version of Linux you use. To enter commands, you'll have to put Linux in command line mode. Linux may be configured to boot to the CLI (Command Line Interface) or the GUI (Graphical User Interface).

If it's configured to boot to the GUI, you can enter command line mode by pressing [Ctrl]+[Alt]+[F1]. Once in the command mode you'll need to enter your username and password before you can enter any commands.

Note Linux doesn't display any feed back, like dots or stars as you enter your password.

In command mode you'll be provided with the command prompt, an example is shown below.

stephen@stephen-Satellite-M35X:~$

Your command prompt will be different than the one shown above. The command prompt consists of your user user name, an @ symbol, and your system's name, a colon (:) symbol, a tilde symbol (~) (tilde represents the top level directory), and a dollar symbol ($). You type your command after the $ symbol.

1. The first command you might enter is pwd (present working directory) as shown below.

stephen@stephen-Satellite-M35X:~$ pwd

When you press the [Enter] key, the command line interpreter will respond with your present location in the directory hierarchy. If this is your first command after logging in, you will probably be in your home directory. An example of a home directory path returned by Linux is shown below.

/home/stephen

Each time Linux responds with a result, it will also provide a fresh prompt, ready for your next command.

2. To change your current working directory to a directory one level higher enter the command cd .. (change directory). Note that there is a space between the cd and the two dots. as shown below.

stephen@stephen-Satellite-M35X:~$ cd ..

The two dots mean to move up one level to the parent directory. Linux will respond to this command by placing the new current directory location in the command prompt as shown below.

stephen@stephen-Satellite-M35X:/home$

3. To get a list of the directories and files in the new current directory, enter the command ls (list sub-directories) as shown below.

stephen@stephen-Satellite-M35X:/home$ ls

In response to this command, Linux will return the name of the directories contained in the current directory as shown below.

stephen

4. To change your current working directory to a sub-directory of the current directory, enter the command cd along with the name of the sub-directory, as shown below.

stephen@stephen-Satellite-M35X:/home$ cd stephen

The sub-directory name that you entered now becomes the current directory.

To determine your location in the directory hierarchy, you can type in the command pwd as often as you need.

5. To get a list of the contents of the current directory, enter the command ls as shown below.

stephen@stephen-Satellite-M35X:~$ ls

In response to this command, Linux will return a list of all the directories and files in the current directory as shown below.

Desktop Documents Downloads examples.desktop
Music Pictures Public Templates Videos

Note that the names of directories are in blue and the names of files are in white.

6. Since I don't have any videos in my home directory, I want to delete the Videos sub-directory. To delete a sub-directory, use the command rmdir (remove directory) followed by the name of the directory to delete as shown below.

stephen@stephen-Satellite-M35X:~$ rmdir Videos

If the directory is not an immediate sub-directory of the current directory, you'll need to type in the full path to the directory.

7. To check if the Videos sub-directory has been deleted you can enter the ls command as shown below.

stephen@stephen-Satellite-M35X:~$ ls

This will return a directories and files listing as shown below.

Desktop Documents Downloads examples.desktop
Music Pictures Public Templates

Note that the sub-directory Videos is now gone.

8. Lets say you changed your mind and you want a Videos directory. To create a sub-directory named Videos in the current directory, you would use the mkdir (make directory) command, as shown below.

stephen@stephen-Satellite-M35X:~$ mkdir Videos

To check if the Videos sub-directory has been created you can enter the ls command as shown below.

stephen@stephen-Satellite-M35X:~$ ls

This will return a directories and files listing as shown below.

Desktop Documents Downloads examples.desktop
Music Pictures Public Templates Videos

Note that the directory Videos is now shown.

Remember; to go up one level use two dots. Every directory has one and only one parent directory. To go to a sub-directory, use the name of the sub-directory.

9. Now lets change our working directory to Documents.

stephen@stephen-Satellite-M35X:~$ cd Documents

Use the ls command to get a listing of the sub-directories and files in the Documents directory.

stephen@stephen-Satellite-M35X:~/Documents$ ls

10. To create and empty text file in the current working directory, type touch followed by the name of the file, an example is shown below.

stephen@stephen-Satellite-M35X:~/Documents$ touch myfile

Entering the ls command now will display myfile in the listing.

myfile

11. To move a file, use the mv (move) command. For example to move myfile to the parent directory of Documents, enter the command shown below.

stephen@stephen-Satellite-M35X:~/Documents$ mv myfile ..

Entering a listing command now will show that myfile is no longer in the Documents directory. If you change your working directory to the parent directory of Documents, and do a listing, you'll see that myfile is in that directory.

Desktop Documents Downloads examples.desktop
Music myfile Pictures Public Templates Videos

12. Next, while in the /home/stephen directory, we can place a copy of myfile back in the Documents directory using the cp (copy) command, as shown below.

stephen@stephen-Satellite-M35X:~/$ cp myfile Documents

But before we navigate to the Documents directory to verify that it contains a copy of myfile, let's first delete the copy of myfile in the current working directory by typing the command shown below.

stephen@stephen-Satellite-M35X:~/$ rm myfile

13.Next, change directory to the Documents directory and do a listing to verify that it contains a copy of myfile.

Lastly, to return to the GUI, press [Ctrl]+[Alt]+[F7]. You'll need to enter your password.

Below is a list of the basic Linux file and directory commands.

CommandFunction
pwddisplay working directory
cd ..change directory to parent directory
cd change to sub-directory
lslist sub-directories and files in directory
mkdir create sub-directory
rmdir delete sub-directory
mv move file to directory
cp copy file to new file
cp copy file to directory
touch create new file
rm myfiledelete file

This article explains only the basic Linux file and directory commands used in their simple forms. There are many more commands and each command has many variations, which make them very powerful.


Learn more at amazon.com

More Windows Administration Information:
• Looking at the Android Operating System
• The Fedora 3 Linux File Structure
• Installing Software on Fedora
• Linux su vs sudo: What's the Difference?
• Setting Up a Linux Modem
• An introduction to the Linux Boot and Startup Processes
• Commands to Display Your Linux Computer's Hardware Information
• Understanding Linux Filesystems: Ext4 and Beyond
• What is Linux?
• How to Load or Unload a Linux kernel Module