Linux command line 101 #1

Published originally here. (English translation in progress)

These small articles are designed to raise awareness of the command line for beginners and as a small reminder for those of us who are comfortable with Linux.

I will present here some commands that are used to navigate the Linux file system. Unlike under Windows, under Linux each available drive on your computer is not represented by a letter (disk c:, d:, E:, etc.). Rather they are all part of the file system. Usually Windows associates the main drive as Disk c:, but linux represented it by /, called the root directory, and the cdrom, floppy drive will be respectively find under /media/cdrom and /media/floppy.

/
|- /bin
|- /etc
...
...
|- /media
    |- /cdrom
    |- /floppy

The command interpreter (usually called “Shell”) has a multiple commands to interact with the file system.

Here are some basic commands :

  • pwd: Return the name and full path of the folder where you are now.
  • cd: Allows you to change your current working directory.
  • ls: Returns the list of items contained in a folder.

By default, when you log on to a terminal with access to the Linux command line, you should find yourself in your home directory. You can check if this is the case by simply typing “pwd” followed by the “Enter” key.

$ pwd
/home/me

From there you can move as you see fit with the command “cd” followed by the name of / folders in which you want to move. This command also uses special characters that have special meanings.

Examples :

/: Root directory.
. : Current directory.
.. : The directory above the current one.
~: The home directory of the user.

Here is a simple example of their usage :

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd .
[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd ..
[moi@monordi ~]$ pwd
[moi@monordi ~]$
/home
[moi@monordi ~]$ cd ~
[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd /usr/bin
[moi@monordi ~]$ pwd
/usr/bin
[moi@monordi ~]$ cd /
[moi@monordi ~]$ pwd
/

You can also simplify things by typing the command “cd” followed by “Enter” to find yourself directly in your personnal home directory.

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd ..
[moi@monordi ~]$ pwd
/home
[moi@monordi ~]$ cd
[moi@monordi ~]$ pwd
/home/moi

You can also type “cd -” to return to the folder in which you were previously.

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd /usr/bin
[moi@monordi ~]$ pwd
/usr/bin
[moi@monordi ~]$ cd -
[moi@monordi ~]$ pwd
/home/moi

It is also possible to move quickly to the personal home directory of another user by using the command “cd” followed by “~” and a user name.

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd ~bob
[moi@monordi ~]$ pwd
/home/bob

We call the absolute path those path that start with the root directory, but them is not mandatory. If you are in the “/home” folder you can type “cd me” instead of “cd /home/me.” Since you do not start with the root directory, the shell will look for the “me” folder at your current position.

[moi@monordi ~]$ pwd
/home/
[moi@monordi ~]$ cd moi
[moi@monordi ~]$ pwd
/home/moi

And you can also mix some special characters to make your own shortcut.

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd ../bob
[moi@monordi ~]$ pwd
/home/bob

Several different commands can give you the same result.

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd ../bob
[moi@monordi ~]$ pwd
/home/bob

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd /home/bob
[moi@monordi ~]$ pwd
/home/bob

[moi@monordi ~]$ pwd
/home/moi
[moi@monordi ~]$ cd ~bob
[moi@monordi ~]$ pwd
/home/bob

The choice is yours.

Now, with the “ls” command you can know the content of the desired directory. This command also uses the same special characters that the “cd” command uses. In fact, they are universal to all commands as they are an integral part of the command interpreter.

Here are some examples :

[moi@monordi ~]$ ls
Desktop Documents Musiques Images Photos Modèles
Vidéos
[moi@monordi ~]$ ls /home/moi
Desktop Documents Musiques Images Photos Modèles
Vidéos
[moi@monordi ~]$ ls /usr/
bin games kerberos libexec sbin src X11R6 etc include
lib local share tmp

This command takes options and arguments to change the information returned. Such as the -l option allows us to have a more detailed lists of the contents of the folder.

[moi@monordi ~]$ ls -l
drw-rw---- 1 moi moi 4096 Jui 8 2012 Desktop
drw-rw---- 1 moi moi 4096 Jui 8 2012 Documents
drw-rw---- 1 moi moi 4096 Jui 8 2012 Musiques
drw-rw---- 1 moi moi 4096 Jui 8 2012 Images
drw-rw---- 1 moi moi 4096 Jui 8 2012 Photos
drw-rw---- 1 moi moi 4096 Jui 8 2012 Modèles
drw-rw---- 1 moi moi 4096 Jui 8 2012 Vidéos

The “ls” command also accepts several arguments such as item names and will return the list of all the contents of each of them.

[moi@monordi ~]$ ls /usr/ /home/moi
/usr/:
bin games kerberos libexec sbin src X11R6 etc include
lib local share tmp
/home/moi/:
Desktop Documents Musiques Images Photos Modèles
Vidéos

To learn more about each of these commands, you can use the “man” command to get the system’s manual followed by the name of the command. This command will show you the list of options and arguments accepted by the command with their explanations. Do not feel embarrassed to use it, it’s free and quite informative.