This is a basic introduction to the command line interface (CLI) in the GNU/Linux operating system. The default shell in Linux is bash, which stands for Bourne Again SHell. The name is a play on the older Bourne shell (sh) found on many Unix OSes.
To begin, some useful terms:
stdin - standard input. This is what is typed at the command line.
stdout - standard output. This is the output from programs to the command line.
arguments - the initial input made when executing a program from the CLI. For example, command arg1 arg2 arg3. command is the name of the program or command, and arg1, arg2, and arg3 are the arguments.
Switches - Arguments that usually turn a feature on or off. Unlike DOS or Windows, Unix programs use - or -- as switch character. For example:
in Unix: cp -Ra ~ /mnt/backup
in DOS: format /s A:
(Those two do entirely different things, I just don't know any other DOS programs' switches)
EOF - a constant representing the end of a file
Comment - a line not interpreted by the shell interpreter (bash). In bash, comments start with a # and go to the end of a line.
Hidden File - A file that is not shown or operated on by default. It's name begins with a .(dot) . Generally, programs have a switch of -a to operate on them.
These will become important later on.
Some basic commands:
echo - prints its arguments it received to stdout, followed by a new line. Example:
cd - changes directories. With no arguments, changes to the user's home directory, represented by the ~ character.Code:echo text text text text text text
cp - copies files. Has many command-line switches that can be usedCode:user@host ~ $ cd / user@host / $ cd /etc/ user@host /etc $ cd user@host ~ $
rm - removes files. Has many switches similar to cp. rmdir removes directories.Code:cp file1 file2 #copies file1 to file2. cp -R dir1/ dir2/ #copies dir1/ to dir2/ #All of the subfolders and files of dir1 are also present under the same names in dir2/ #the -R stands for recursive, it is needed when copying directories
ls - lists files has many possible switches. Some commonly used ones are:Code:rm file1 #file1 is gone rmdir dir1/ #dir1 is gone. dir1 must be empty or else this will fail rm -rf /dir1 #dir1 is gone, including all subfolders. This works for removing non-empty directories #-r stands for recursive, -f for force #the -r in rm is lowercase, for cp -R is uppercase rm -rf / #DO NOT attempt this as the root user. it deletes all files on all mounted filesystems
-l - long version
--color - coloured output, usually aliased to ls
-F - outputs extra characters such as a / after directory names.
-a - shows hidden files (which begin with a .)
mv - moves files. Unlike rm or cp, has no -rR option for recursion. It moves entire directories and all subfolders. Is also used to rename files or directories without actually moving them anywhere else in the directory structure.Code:ls #shows the contents of the current directory ls ~ #shows the contents of the user's home directory ls -lFa --color #combines some commonly used switches
alias - makes a shortcut command. These are usually put into the user's ~/.bashrc file.Code:mv dir1/ dir2/ #essentially renames dir1/ to dir2/ mv ~/dir1/dir2/file1 . #moves file1 from ~/dir1/dir2 to the present directory (represented by the '.' (dot))
mkdir - makes a new directory. A useful switch is the -p, creating a direcory and all its parent directories.Code:alias ls="ls --color -F" #now whenever ls is typed,it is interpreted as ls --color -F
pwd - outputs the path to the current directory (pwd = present working directory)Code:mkdir test #makes a directory called test mkdir -p a/b/c/d #creates a directories a, b, c, and d. d/ is inside c/ is inside b/ is inside a/
cat - concatenates files. commonly used to output files to stdout. Can be used as a basic text editor. Examples:Code:cd #remember, no arguments results in changing to ~ (home directory) pwd /home/user/
zcat, bzcat - Perform the same function as cat except they work on compressed files. Zcat is for gzip files and bzcat for bzip2 files. A common usage of this could be to output the configuration of the running kernel.Code:cat /path/to/a/file file contents cat /etc/file > ~/file #contents of /etc/file are now in ~/file #anything that was in ~/file was truncated (erased) cat /etc/file >> ~/file #contents of /etc/file were appended to ~/file #whatever was in ~/file remains with /etc/file after it #using cat as a text editor cat > text <<EOF > this is > a test > EOF #the file text now contains this is\na test\n #\n indicates a new line or line break <br /> is the HTML equivalent
Code:zcat /proc/config.gz #outputs the configuration of the running linux kernel
head, tail - head outputs the beginning of a file, tail the end. The -n option allows you to specify the number of lines. The default is 10 lines
Code:head file1 #outputs the first 10 lines of file1 tail -n 100 file1 #outputs the last 100 lines of file1
tar - Creates an archive of files. It offers no compression. Thus, it is commonly used with bzip2 or gzip. Examples:
gzip, bzip2, gunzip, bunzip2 - common compression tools. They compress or decompress files. It is common to use them in conjunction with tar.Code:#extracting a file tar xvzpf file.tar.gz #the xvzpf switches say: #x - extract the file #v - verbose output, not really necessary, but nice to see #z - use gzip decompression, could be substituted for j for bzip2 #p - preserve permissions, often necessary if you are extraction important system files, #like in a gentoo linux install #f - use a file, not stdin as input. #creating a tar archive tar cvzpf archive.tar.gz directory1/ directory2/ #c - creates an archive #z - compresses the archive with gzip #vpf - same as above #it is important that the name of the archive come before the files to create it from #tar will not create a new directory in the archive. When this is extracted, it will create #directories directory1/ and directory2/
chmod - Changes the "mode" of a file. Example:Code:gzip archive.tar #compresses archive.tar, changes file name to archive.tar.gz gunzip archive.tar.gz #decompressed archive.tar.gz, leaving archive.tar tar xvpf archive.tar #extracts archive.tar #Notice the absence of the z option. It is not needed as the file is not compressed.
Some other important tips and tricks:Code:chmod +x file.sh #adds the x (eXecutable) flag. file.sh could now be executed by typing ./file.sh
.(dot) - a symbolic link in every directory referring to itself. For example
..(dotdot) - a symbolic link in every directory pointing to its parent directoryCode:cd . #does nothing, changes to the current directory
~(tilde) - refers to the users home directoryCode:pwd /home/user/dir1 cd .. pwd /home/user
Code:cp ~/file1 ./file1 #copies file1 from users home dir (~) to the present one.
.bashrc - a file that exists in the user's home (~) directory. Executed as a shell script whenever bash is started. Often used to alias commands.
Tab completion - if you start typing the name of a file, and press the tab key, the name will be finished. If there are multiple possibilities, the PC speaker will make a beep. If you press the tab key twice quickly with multiple possibilities, all of the possibilities will be outputted.
Copy and Paste - non-graphical terminals do not support the Ctrl-C and Ctrl-v method of copying and pasting. X11 (the windowing environment) does, so this can be used with certain graphical terminals. Gnome-terminal and KDE's konsole most likely support this. However, a simple xterm (like I use) or a non-graphical tty terminal does not.
Copying and pasting with these terminals requires a three button mouse. The scroll wheel can often function as a button.
To copy from a terminal: just highlight the text you want copied. The copy action is implied and no further action is needed.
To paste to a terminal or other application: click the middle mouse button. This can be used to copy from a terminal to a web browser or any graphical application as well. In a bash shell, line breaks are interpreted as though you are entering command. If you are pasting to command line editor or other command line program, it works as expected.
To paste to a terminal from a graphical application: Do a normal copy to clipboard (highlight text + press Ctrl-C). Then, you can paste to a terminal with the middle mouse button.
Phew, that took me a while to write.


LinkBack URL
About LinkBacks
Reply With Quote





