Basic commands

  • Evolve in the tree and file manipulation
    • Current directory display (pwd)
    • Directory change (cd)
      • Relative and absolute path
    • List of files in a directory (ls)
    • Viewing the contents of a file
      • View all the file (cat)
      • View the file with a page (less / more)
      • View the beginning and end of a file (head / tail)
    • Directory creation (mkdir)
    • Copy of file or directory (cp)
    • Delete file or directory (rm)
    • File move or rename (mv)
    • Minimalist editor (pico / nano)

Evolve in the tree and file manipulation

Show current directory (pwd)

To know which directory you are in, you can use the pwd (print working directory) command:

print working directory

1
2
$ pwd
/home/Bob
$ pwd /home/Bob

Change directory (cd)

The cd (change directory) command allows you to change directories,

command cd

1
2
3
4
5
6
7
8
9
10
$ cd . # . refers to the current directory
$ cd .. # .. designates the parent directory
$ cd / # / is the root directory
--------------------------------------------------------------------------------------------------
$ cd /tmp # designates the directory tmp belonging to the root
$ cd tmp # is the tmp directory of the current directory
$ cd ../tmp # is the tmp directory of the parent directory of the current directory
-------------------------------------------------------------------------------------------------
$ cd ~ # return to his home directory
$ cd # same

Relative and absolute path

  • The absolute path always refers to the root (represented by the / slash character) of the system on which the folder tree with which one works is located.
  • The relative path, as its name indicates, refers to the position relative to the file where you are
For example if I am in the directory / home /, represented in blue opposite,
the command prompt will be represented as follows:
 
1
user@hostname:/export/home$
If I want to go to the mary directory, I can use absolute or relative notation. The following set of commands are equivalent:
relative and absolute path
1
2
3
4
5
6
# notation absolute
user@hostname:/export/home$ cd /export/home/mary
# notation relative
user@hostname:/export/home$ cd mary
user@hostname:/export/home$ cd ./mary
user@hostname:/export/home$ cd ../../export/home/mary

List files in a directory (ls)

the ls (list) command lists the contents of the directory. A large number of options exist for this command, to facilitate the display of the result. By default, ls list the contents of the current directory, but it is also possible to provide the directory or file to list.

command ls

1
2
3
4
5
6
7
8
9
# List the contents of the current directory /home/alex
user@hotname:/home/alex$ ls
Desktop Downloads Music Public Videos
Documents fichiers Pictures Templates

# List the contents of the directory /tmp
user@hotname:/home/alex$ ls /tmp
pulse-2L9K88eMlGn7 tmpksCRJ5 ssh-BkXkFipk2242 fichiers_temporaire
un_autre_exemple
Interesting option:
  • -l (long): detailed list of files. Displays permissions, owner, file size and date

    command ls option long

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    user@hostname:/home/alex $ ls -l /etc/
    total 1636
    drwxr-xr-x 3 root root 4096 Mar 12 2013 acpi
    -rw-r--r-- 1 root root 2981 Apr 25 2011 adduser.conf
    drwxr-xr-x 2 root root 32768 Nov 7 16:06 alternatives
    -rw-r--r-- 1 root root 395 Jun 20 2010 anacrontab
    drwxr-xr-x 7 root root 4096 Jul 26 16:01 apache2
    -rw-r--r-- 1 root root 112 Jun 22 2007 apg.conf
    drwxr-xr-x 6 root root 4096 Apr 25 2011 apm
    drwxr-xr-x 3 root root 4096 Sep 20 08:19 apparmor
    drwxr-xr-x 8 root root 4096 Nov 5 09:47 apparmor.d
    drwxr-xr-x 5 root root 4096 Nov 5 09:46 apport
    drwxr-xr-x 6 root root 4096 Oct 23 13:03 apt
    -rw-r----- 1 root daemon 144 Jun 27 2010 at.deny
    drwxr-xr-x 2 root root 4096 Aug 24 2012 at-spi2
    drwxr-xr-x 3 root root 4096 Aug 24 2012 avahi
    -rw-r--r-- 1 root root 9085 Jun 12 2012 avserver.conf
    -rw-r--r-- 1 root root 2076 Apr 3 2012 bash.bashrc
    -rw-r--r-- 1 root root 58753 Oct 4 2011 bash_completion
    drwxr-xr-x 3 root root 16384 Nov 7 16:06 bash_completion.d
    drwxr-sr-x 2 root bind 4096 Aug 7 16:35 bind
    ....... [texte tronqué] .....
  • -a (all): lists all files, including "hidden" files. A "hidden" file is a file that starts with a "."

    ls option all

    1
    2
    3
    user@hotname:/home/alex$ ls -a
    Desktop .bashrc .bash_profile Downloads Music Public Videos
    Documents fichiers Pictures Templates .app .gnupg
  • --color = auto / none: Allows to display or not the colors with ls, the majority of the distributions activate the coloring by default
Option Combined, it is possible to combine options:
  • -l -t -r or -ltr: Display the result in long format (-l), Sort the result by the modification date of the file unlike the default name (-t) the most recent file will be displayed first the oldest last, reverse the result (-r) this in order to have the most recent file displayed last.

    ls with sort

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # without reverse
    user@hostname:/home/alex$ ls -lt /etc
    ...... [texte tronqué] ...
    drwxr-xr-x 3 root root 4096 May 9 2010 insserv
    -rw-r--r-- 1 root root 645 Mar 7 2010 ts.conf
    -rw-r--r-- 1 root root 15752 Jul 25 2009 ltrace.conf
    -rw-r--r-- 1 root root 112 Jun 22 2007 apg.conf
    -rw-r--r-- 1 root root 10852 Apr 27 2007 gnome-vfs-mime-magic
    -rw-r--r-- 1 root root 1343 Jan 9 2007 wodim.conf
    -rw-r--r-- 1 root root 2064 Nov 23 2006 netscsid.conf
    user@hotname:/home/alex$
    
    # with it :)
    user@hostname:/home/alex$ ls -ltr /etc
    ...... [texte tronqué] ...
    -rw------- 1 root root 1822 Nov 29 15:51 shadow-
    -rw------- 1 root root 1164 Nov 29 15:51 group-
    -rw------- 1 root root 959 Nov 29 15:51 gshadow-
    -rw-r--r-- 1 root root 1176 Nov 29 15:51 group
    -rw------- 1 root root 2650 Nov 29 15:51 passwd-
    -rw-r----- 1 root shadow 967 Nov 29 15:51 gshadow
    -rw-r----- 1 root shadow 1944 Nov 29 15:51 shadow
    -rw-r--r-- 1 root root 2653 Nov 29 15:51 passw
  • -l --block-size = M or -h: Display the result in long format (-l) and provide the file size in Megs, much easier to read

ls option size in meg

1
2
3
4
5
user@hostname:~$ ls -l -h
drwxr-xr-x 6 x x 4.0K Jul 22 21:19 gits
-rw-r--r-- 1 x x 1.7K Mar 23 2013 GNUstepDefaults
-rw-r--r-- 1 x x 33M Sep 17 12:31 go.tar.gz
-rw-rw-r-- 1 x x 87K Nov 21 12:53 hand_spacewalk.png

View the contents of a file

View all the file (cat)

The cat (concatenate) command displays the contents of a file or multiple concatenated files on the screen.

command cat

1
2
3
4
5
6
7
8
9
10
user@hostname:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
....... [texte tronqué] .....
statd:x:114:65534::/var/lib/nfs:/bin/false
postfix:x:115:124::/var/spool/postfix:/bin/false
user@hostname:~$
Interesting option:
  • -n or --number (number): to display the line number

command cat option number

1
2
3
4
5
6
7
8
9
user@hostname:~$ cat -n /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3 bin:x:2:2:bin:/bin:/bin/sh
4 sys:x:3:3:sys:/dev:/bin/sh
5 sync:x:4:65534:sync:/bin:/bin/sync
....... [texte tronqué] .....
35 statd:x:114:65534::/var/lib/nfs:/bin/false
36 postfix:x:115:124::/var/spool/postfix:/bin/false

View the file with a page (less / more)

The problem with cat is that it can be complicated to view a file if it is very large. Indeed, cat will roll out the terminal from the beginning to the end; it can be difficult to read. To solve this problem, we have less and more available to us. Personally, I use a lot more than more, because the first to more options than the second: P.

command less et more

1
2
3
4
5
6
7
8
# use of less
user@hotname:~$ less /var/log/dmesg

# use of less , show numbers lines
user@hotname:~$ less -N /var/log/dmesg

# use of more
user@hotname:~$ more /var/log/dmesg

View the beginning and end of a file (head / tail)

We do not always want to have an entire file. At times, displaying an X number of lines at the beginning or end of a file may be sufficient. The command head and tail, with the option -n (number), allows us to have this behavior. By default, if the number of lines is not specified, the system displays 10 lines.

command head and tail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# HEAD
user@hostname:~$ head -n 4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh

# TAIL
user@hostname:~$ tail /etc/passwd
rtkit:x:106:116:RealtimeKit,,,:/proc:/bin/false
sshd:x:107:65534::/var/run/sshd:/usr/sbin/nologin
mysql:x:108:117:MySQL Server,,,:/nonexistent:/bin/false
minidlna:x:109:119:MiniDLNA server,,,:/var/lib/minidlna:/usr/sbin/nologin
ntp:x:110:120::/home/ntp:/bin/false
fetchmail:x:111:65534::/var/lib/fetchmail:/bin/false
tester:x:1001:1001:,,,:/home/tester:/bin/bash
avahi:x:112:121:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
statd:x:114:65534::/var/lib/nfs:/bin/false
postfix:x:115:124::/var/spool/postfix:/bin/false
Interesting option of tail:
  • -f or --follow: to display on the screen the lines that are added to the file continuously. This is VERY interesting, mainly for log visualization.

Directory creation (mkdir)

The creation of a directory can be realized by using the command mkdir (make directory), giving the name of the directory to be created in argument, as follows

command mkdir

1
2
3
4
5
6
# Notation absolute
user@hostname:/home/user$ mkdir /tmp/Nom_du_repertoire

# Notation relative
user@hostname:/home/user$ mkdir Nom_du_repertoire
user@hostname:/home/user$ mkdir ../../tmp/Nom_du_repertoire
Interesting option:
  • -p: create a directory tree faster; mkdir, by default, only creates one directory at a time.
    For example, if I'm in the directory / home / alex and I want to create the directory hierarchy / home / alex / images / 2013/06/18,
    here's how to do it:

mkdir option parent

1
2
3
4
5
6
# Command on ERROR
user@hotname:/home/alex$ mkdir images/2013/06/18
mkdir: cannot create directory `images/2013/06/18': No such file or directory

#solution
user@hotname:/home/alex$ mkdir -p images/2013/06/18

Copy of file or directory (cp)

The command cp (copy) allows us to copy one or more files or directories.

command cp

1
2
3
4
5
6
7
8
9
10
11
# Copy the file passwd in /tmp
user@hotname:/home/alex$ cp /etc/passwd /tmp

# Copy the file passwd and change the file name for copie_pass
user@hotname:/home/alex$ cp /etc/passwd /tmp/copie_pass

# Copy of several files into a directory, when you copy several files the destination must be a directory
user@hotname:/home/alex$ cp /etc/passwd /etc/fstab /etc/group /tmp/

# Copy a directory with the -r recursive option
user@hotname:/home/alex$ cp /etc/network /tmp
Interesting options:
  • -r (recursive): as presented earlier, allows us to recursively copy files
  • -l or --link (link): do not copy the actual file, but create a hard link from the file to the corresponding inode on the disk.
  • --parents: this option allows us to tell the cp command to recreate the directory structure when copying; Saving the use of the mkdir directory creation command

command cp option parent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
user@hotname:/home/alex$ ls -R /tmp/testing
user@hotname:/home/alex$ cp --parent /usr/share/debianutils/shells /usr/share/gimp/2.0/images/gimp-splash.png /etc/network/interfaces /tmp/testing/
user@hotname:/home/alex$ ls -R /tmp/testing
/tmp/testing/:
etc usr

/tmp/testing/etc:
network

/tmp/testing/etc/network:
interfaces

/tmp/testing/usr:
share

/tmp/testing/usr/share:
debianutils gimp

/tmp/testing/usr/share/debianutils:
shells

/tmp/testing/usr/share/gimp:
2.0

/tmp/testing/usr/share/gimp/2.0:
images

/tmp/testing/usr/share/gimp/2.0/images:
gimp-splash.png

Delete file or directory (rm)

The rm (remove) command allows file or directory deletion

command rm

1
2
3
4
5
6
# delete the file photo1.png
user@hotname:/home/alex$ rm Lefichier

# for directory deletion add the -r option for recursive
# here the deletion is done on the directory photos_noel
user@hostname:/home/alex$ rm -r photos_noel

 

File move or rename (mv)

The mv (move) command lets you move files from one directory to another or rename the file

command mv

1
2
3
4
5
# Move a file from ome directory to another
user@hotname:/home/alex$ mv /home/alex/Documents/billetterie.png Projets/GrosProblem/

# rename a file
user@hotname:/home/alex$ mv Projets/GrosProblem/billetterie.png Projets/GrosProblem/loterie.png

Minimalist editor (pico / nano)

Waiting to present a "real" text editor of at least one of the most powerful available on GNU / Linux and Unix ie VI you can use pico or nano both make calls to the same binary during the installation under Ubuntu. nano offers you the equivalent of notepad, a simple text editor to edit files here's what it looks like:

command pico

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
user@hostname:~$ nano /etc/passwd
....... [texte tronqué] .....
sshd:x:107:65534::/var/run/sshd:/usr/sbin/nologin
mysql:x:108:117:MySQL Server,,,:/nonexistent:/bin/false
minidlna:x:109:119:MiniDLNA server,,,:/var/lib/minidlna:/usr/sbin/nologin
ntp:x:110:120::/home/ntp:/bin/false
fetchmail:x:111:65534::/var/lib/fetchmail:/bin/false
tester:x:1001:1001:,,,:/home/tester:/bin/bash
avahi:x:112:121:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
statd:x:114:65534::/var/lib/nfs:/bin/false
postfix:x:115:124::/var/spool/postfix:/bin/false





[ Read 36 lines (Warning: No write permission) ]
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell