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:
- The Present Working Directory
pwd
Using flags¶
- Many *nix commands support the use of
flagsthat 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
manor 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¶
mkdirto create a directory. Use the-poption to create subdirectoriesrmdirorrm -frwill 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 ~]$
Relative Paths¶
.in *nix denotes the current working directory..denotes the parent directory/is used to indicate the path relative to.
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
lesscommand allows the user to view the output forwards and backwards
The file command¶
- Used to identify the type of file (binary, text etc)
Redirecting output¶
>,>>&<are used to redirect standard output2>is used to redirect standard error
Note: Observe that the output is not sent to the screen. It is instead, redirected to the file called greetings.txt
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
Copying, moving and deleting files¶
cpcommand is used to copy a file.- Using this command with the
-Rflag allows recursive copy - copying a directory structure.
- Using this command with the
mvcommand is used to rename a directory or file.rmcommand is used to remove a file.- Used along with the
rflag removes recursively. - Used along with the
fflag removes forcibly.
- Used along with the
Lab Time¶
- Lab 3 - Viewing files, redirection and chaining
- Lab 4 - Copying, moving and deleting
Editing text¶
- The
catcommand can be used with redirection to create or update files. - Close out files using the
CTRL-dkey combination.