Skip to content

Getting around in the Linux OS

Introduction to Linux for Network Engineers

Directory Listing

Because memory and CPU power were at a premium in the past, UNICS (eventually shortened to UNIX) used short commands to minimize the space needed to store them and the time needed to decode them - hence the tradition of short UNIX commands we use today, e.g. ls, cp, rm, mv etc.

  • List contents of the directory:
[ntc@ntc ~]$ ls
configs  VirtualBoxVMs
[ntc@ntc ~]$ 
  • The Present Working Directory pwd
[ntc@ntc ~]$ pwd
/home/ntc
[ntc@ntc ~]$

Using flags

  • Many *nix commands support the use of flags that extend behavior
# Long  Listing

[ntc@ntc ~]$ ls -l
total 0
drwxrwxr-x. 3 ntc ntc 51 Jan 22 19:56 configs
drwxrwxr-x. 3 ntc ntc 45 Jan 23 17:02 VirtualBoxVMs
[ntc@ntc ~]$ 
# Long listing including hidden files, latest files last.

[ntc@ntc ~]$ ls -latr
total 24
-rw-r--r--. 1 ntc  ntc   231 Dec  6  2016 .bashrc
-rw-r--r--. 1 ntc  ntc   193 Dec  6  2016 .bash_profile
-rw-r--r--. 1 ntc  ntc    18 Dec  6  2016 .bash_logout
drwxr-xr-x. 4 root root   32 Dec 21 16:37 ..
-rw-rw-r--. 1 ntc  ntc    40 Jan 22 19:48 .gitconfig
drwxrwxr-x. 3 ntc  ntc    51 Jan 22 19:56 configs
-rw-------. 1 ntc  ntc    41 Jan 22 19:56 .lesshst
drwx------. 3 ntc  ntc    24 Jan 23 16:59 .config
drwxrwxr-x. 7 ntc  ntc   119 Jan 23 17:00 .vagrant.d
drwxrwxr-x. 3 ntc  ntc    45 Jan 23 17:02 VirtualBoxVMs
-rw-------. 1 ntc  ntc  2094 Jan 24 13:33 .bash_history
drwx------. 6 ntc  ntc   186 Jan 24 17:21 .
[ntc@ntc ~]$ 

man pages

  • In-built help is provided by invoking the man or manual command
LS(1)                                                            User Commands                                                           LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about  the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is
       specified.

.
.
.
.
.
<output truncated for readability>

Directory Creation/Deletion

  • mkdir to create a directory. Use the -p option to create subdirectories
  • rmdir or rm -fr will remove directories
[ntc@ntc ~]$ mkdir tux

[ntc@ntc ~]$ ls -ltr
total 0
drwxrwxr-x. 3 ntc ntc 51 Jan 22 19:56 configs
drwxrwxr-x. 3 ntc ntc 45 Jan 23 17:02 VirtualBoxVMs
drwxrwxr-x. 2 ntc ntc  6 Jan 24 17:53 tux
[ntc@ntc ~]$ 
[ntc@ntc ~]$ mkdir -p tux/level-1/level-2/level-3

Relative Paths

  • . in *nix denotes the current working directory
  • .. denotes the parent directory
  • / is used to indicate the path relative to .
[ntc@ntc level-3]$ cd ../../../../../../home
[ntc@ntc home]$ pwd
/home

Lab Time

  • Lab 1 - Accessing the lab
  • Lab 2 - File System Navigation

Viewing file contents, redirection and command chaining

Introduction to Linux for Network Engineers

The cat command

  • Stands for "concatenate" files and print to standard output
  • Used typically to display file contents or stream them to another command
[ntc@ntc ~]$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
.
.
.
.
.
<output truncated for readability>

more and less

  • Commands to allow the user to paginate the contents of a lengthy file
  • The less command allows the user to view the output forwards and backwards
[ntc@ntc ~]$ less /etc/services
[ntc@ntc ~]$ more /etc/services 

The file command

  • Used to identify the type of file (binary, text etc)
[ntc@ntc ~]$ file tux
tux: directory
[ntc@ntc ~]$ 
[ntc@ntc ~]$ file /etc/services 
/etc/services: C source, ASCII text
[ntc@ntc ~]$ 

Redirecting output

  • >, >> & < are used to redirect standard output
  • 2> is used to redirect standard error
[ntc@ntc ~]$ echo "Hello, World :)" > greetings.txt
[ntc@ntc ~]$ 

Note: Observe that the output is not sent to the screen. It is instead, redirected to the file called greetings.txt

[ntc@ntc ~]$ cat greetings.txt 
Hello, World :)
[ntc@ntc ~]$ 

Redirect output and errors into separate files

[ntc@ntc ~]$ cat tux/directory_listing.txt > output 2>error
[ntc@ntc ~]$ 
[ntc@ntc ~]$ cat error
[ntc@ntc ~]$ 
[ntc@ntc ~]$ cat output
total 4
drwxrwxr-x. 3 ntc ntc 51 Jan 22 19:56 configs
drwxrwxr-x. 3 ntc ntc 45 Jan 23 17:02 VirtualBoxVMs
drwxrwxr-x. 3 ntc ntc 50 Jan 25 15:44 tux
-rw-rw-r--. 1 ntc ntc 16 Jan 25 15:48 greetings.txt
[ntc@ntc ~]$ 
[ntc@ntc ~]$ 

Redirect output and errors into the same file:

[ntc@ntc ~]$ cat tux tux/directory_listing.txt >output  2>&1 
[ntc@ntc ~]$ 

[ntc@ntc ~]$ cat output 
cat: tux: Is a directory
total 4
drwxrwxr-x. 3 ntc ntc 51 Jan 22 19:56 configs
drwxrwxr-x. 3 ntc ntc 45 Jan 23 17:02 VirtualBoxVMs
drwxrwxr-x. 3 ntc ntc 50 Jan 25 15:44 tux
-rw-rw-r--. 1 ntc ntc 16 Jan 25 15:48 greetings.txt
[ntc@ntc ~]$ 

Using "pipes" to chain commands

  • "Pipe" denoted by the | symbol is used to send the output of one command as an input to another
[ntc@ntc ~]$ ls -l /usr/bin/ | more
total 72192
[ntc@ntc ~]$ ls -l tux/ | wc -l > file_count.txt
[ntc@ntc ~]$ 

Copying, moving and deleting files

  • cp command is used to copy a file.
    • Using this command with the -R flag allows recursive copy - copying a directory structure.
  • mv command is used to rename a directory or file.
  • rm command is used to remove a file.
    • Used along with the r flag removes recursively.
    • Used along with the f flag removes forcibly.
[ntc@ntc ~]$ cp nyc-rtr01.cfg nyc-rtr01_backup.cfg
[ntc@ntc ~]$
[ntc@ntc ~]$ cp -r tux tux-backup
[ntc@ntc ~]$
[ntc@ntc ~]$ mv nyc-rtr01_backup.cfg nyc-rtr01.cfg
[ntc@ntc ~]$ rm -r tux-backup

Lab Time

  • Lab 3 - Viewing files, redirection and chaining
  • Lab 4 - Copying, moving and deleting

Editing text

  • The cat command can be used with redirection to create or update files.
  • Close out files using the CTRL-d key combination.
[ntc@ntc ~]$ cat > change_notes.txt
Interfaces to add to NYC-RTR01:
==============================
1. Loopback 1  
2. Tunnel 101
3. Portchannel 10
[ntc@ntc ~]$
[ntc@ntc ~]$ cat >> change_notes.txt
4. Loopback 2
[ntc@ntc ~]$