Training/Linux - command line
Contents
Introduction
This represents a series of teaching pages to allow you to learn more about Viper's Linux command line interface
Command Line
Like other operating systems Linux does have a window’s type environment too called X and hopefully in the future its successor Wayland. (Linux refers to X and Wayland with the term Display Server). However, with VIPER the vast majority of work will be carried out on the command line.
The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style there are a few different ones to choose from, however the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
Man pages
This will explain the use of man pages (also called manual pages) on Linux. Most Linux files and commands have pretty good man pages to explain their use. Type man followed by a command (for which you want help) and start reading. Press q to quit the man page. See below:
username@viper:~$ man whois' (shows manual page for the command whois) username@viper:~$ man syslog.config' (shows the manual page for a configuration file) username@viper:~$ man syslogd (show the manual for a daemon (background program)) username@viper:~$ man –k syslog (an apropos which shows a list of available man pages with this string contained within it)
Working with directories
As with other operating systems such as Microsoft Windows the filesystem is based around files and directories. Linux is no exception to this and uses a number of commands for the user to navigate around its own filesystem.
This module is a brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux system.
This section will also discuss absolute and relative paths and path completion in the bash shell.
pwd
On the command line pwd (or print working directory) basically displays the current directory you are in. This would appear as:
username@viper:~$ pwd /home/dbird
cd
On the command line cd (or change directory) changes your current directory to the one specified:
username@viper:~$ cd /var username@viper:~$ pwd /var username@viper:~$ cd /home/user username@viper:~$ pwd /home/user
There is also a shortcut back to your home directory by typing the character ~ (tilda) which has the same effect as typing (for example) /home/user.
username@viper:~$ cd ~ username@viper:~$ pwd /home/user
To go to the directory above (or parent directory), we use the characters ..
username@viper:~$ pwd /home/user username@viper:~$ cd .. username@viper:~$ pwd /home
There is also the character . which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.
Absolute and relative paths
Although an important concept to understand the rule here is very simple. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point.
Here are two examples, firstly of absolute path
username@viper:~$ pwd /home/user user@viper:~$ cd /var user@viper:~$ pwd /var
And relative path
username@viper:~$ pwd /home/user username@viper:~$ cd myfiles username@viper:~$ pwd /home/user/myfiles
The command line will help you in typing a path without errors. If you type a partial command such as:
username@viper:~$ cd /home/user/my
Pressing the TAB key will fill in the rest of the directory name, if that directory exists and it is unique). So the following would appear
username@viper:~$ cd /home/user/myfiles
ls
This command lists the contents of a directory:
username@viper:~$ ls myfile1.txt myfile2.txt mydirectory1
ls has a number of useful different options
username@viper:~$ ls –l (show a long listing with more information) username@viper:~$ ls –a (show all files including those that are hidden) username@viper:~$ ls –la (combines both of the options above)
mkdir
This commands makes a directory from the specified directory:
username@viper:~$ mkdir mydirectory1 username@viper:~$ cd mydirectory1 username@viper:~$ pwd /home/user/mydirectory1
rmdir
This command removes the specified directory, note the directory must by empty and must not be the directory you are currently in:
username@viper:~$ rmdir mydirectory1
Working with files
In this section we learn how to recognise, create, remove, copy and move files using commands like file, touch, rm, cp, mv and rename.
Just before we start looking at these commands we need to cover two important areas:
- Files on Linux are case sensitive. This means that FILE1 is different from file1, and /etc/hosts is different from /etc/Hosts (the latter one does not exist on a typical Linux computer).
- Everything on Linux is a file. A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1) and any process are all represented somewhere in the file system as a file.
file
This command determines the file type. Unlike Windows, Linux does not determine the file type from the extension but from examining the file header/contents itself.
username@viper:~$ file mypicture.png pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced username@viper:~$ file parallel.c parallel.c: ASCII C program text
touch
This creates an empty file, which can be useful for various uses.
username@viper:~$ touch newfile.c username@viper:~$ ls newfile.c
rm
Remove a file, as always be very careful with this command and without a backup this file will be lost forever.
username@viper:~$ ls newfile.c backup.c username@viper:~$ rm newfile.c username@viper:~$ ls backup.c
username@viper:~$ rm –i backup.c (this is interactive delete and will ask you to delete the file before it occurs) username@viper:~$ rm –r mydirectory (works recursively down the specified directory but not removing any non-empty directories) username@viper:~$ rm –rf mydirectory (works recursively down the specified directory however the –f option means force. This option is very powerful but also needs to be used with extreme care!)
As with many Linux commands there are a few options with can be used with rm (these can be view by typing man rm).
cp
Copy files or directories from a source to a destination:
username@viper:~$ cp parallel.c mybackup.c (copies parallel.c to mybackup.c) username@viper:~$ cp parallel.c mydirectory1 (copies parallel.c to mydirectory1) username@viper:~$ cp *c backupdirectory/ (copies all *.c files to backupdirectory) username@viper:~$ cp –r mydirectory1 mydirectory2 (copies one directory to another, note the option –r for recursive copying)
As with many Linux commands there are a few options with can be used with cp (these can be view by typing man cp).
mv
Move files from a source to a destination. A versatile command that can rename a file too:
username@viper:~$ mv file1.c testfile.c (rename file1.c to testfile.c) username@viper:~$ mv directory1 directory2 (rename directory) username@viper:~$ mv file1.c /home/user/myrepo (mv file1.c to /home/user/myrepo/file1.c)
rename
Although preferably to use the mv command, this command does exist to rename files
username@viper:~$ touch file1.c file2.c file3.c username@viper:~$ ls file1.c file2.c file3.c username@viper:~$ rename .c .backup *.c username@viper:~$ ls file1.backup file2.backup file3.backup
Working with file contents
This section will look at working with file contents themselves, such commands are head, tail, cat, tac, more and less.
head
By default head will show the first ten lines of a file.
username@viper:~$ head /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 games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh
tail
Similar to head but this time it will show the last 10 lines of file by default.
username@viper:~$ tail /etc/services vboxd 20012/udp binkp 24554/tcp # binkp fidonet protocol asp 27374/tcp # Address Search Protocol asp 27374/udp csync2 30865/tcp # cluster synchronization tool dircproxy 57000/tcp # Detachable IRC Proxy tfido 60177/tcp # fidonet EMSI over telnet fido 60179/tcp # fidonet EMSI over TCP