Introduction to Unix CLI: Difference between revisions
| Line 182: | Line 182: | ||
===grep=== | ===grep=== | ||
===tail=== | ===tail=== | ||
''tail'' outputs the last part (by default last 10 lines) of the file. | |||
<pre> | |||
$ # show last 25 lines from messages file | |||
$ tail -25 /var/log/messages | |||
$ # follow file output, output will continue until it is | |||
$ # broke with Ctrl+C | |||
$ tail -f /var/log/messages | |||
</pre> | |||
===head=== | ===head=== | ||
===awk=== | ===awk=== | ||
Revision as of 19:19, 28 October 2015
What is Unix CLI?
Unix CLI is actually the shell, a text interface to the operating system. Shell interprets and executes your commands and their arguments by passing them to the operating system.
There are different shell variants, like bash, csh, ksh, sh, tcsh, etc., probably the most popular being bash. The name “bash” is an acronym for “Bourne Again SHell” and is an enhanced replacement for sh. In this short introductory we are going to cover bash.
Bash
Bash is an interactive and scriptable shell. Supports name expansion, wildcards and typing shortcuts.
Note: Interaction with bash is case-sensitive just like everything in Unix system.
Some useful shortcuts:
TAB, UP, DOWN, Ctrl+R, Esc+.
Popular commands
man
man is the system's manual pager. It is an interface to on-line manuals. man has it's own manual pager.
$ # Show default man page for top command $ man sleep $ # Show all manual pages for top command, successively $ man -a sleep
Whenever you are uncertain what a command does, what options and argument can be passed to, man is your friend.
apropos
apropos searches the manual page names and descriptions for the specified keyword.
$ apropos shell bash (1) - GNU Bourne-Again SHell bash [sh] (1) - GNU Bourne-Again SHell capsh (1) - capability shell wrapper chroot (1) - run command or interactive shell with special root directory chsh (1) - change your login shell …
pwd
pwd prints the full name of the current working directory.
$ pwd /tmp/test/course2015
cd
cd changes the current working directory to the specified directory name. If no directory is specified than cd changes current working directory to users home directory. cd supports relative and absolute paths. Additionally shortcuts like ~ (points to user's home directory).
$ pwd /tmp/test/course2015 $ cd bin $ pwd /tmp/test/course2015/bin $ cd ../../src $ pwd /tmp/test/src $ cd ~/bin $ pwd /home/lorand/bin $ # Change back to previous directory $ cd -
mkdir
mkdir command creates directories if they do not exist.
$ mkdir build
To create a whole directory structure, use "-p" option.
$ mkdir -p ~/build/wrf/3.7
ls
ls command lists content of the current or the specified directory. One of the most used options for ls is "-l", which will generate a long list format.
$ ls GConf libegroupwise-1.2.so.13 libospf.so.0.0.0 ImageMagick-6.4.3 libegroupwise-1.2.so.13.0.1 libossp-uuid.so.16 Mcrt1.o libelf-0.152.so libossp-uuid.so.16.0.22 PackageKitDbusTest.py libelf.so.0 libp11.so.1 … $ ls -l total 806992 drwxr-xr-x 3 root root 4096 Mar 11 2012 GConf drwxr-xr-x 4 root root 4096 Nov 18 2014 ImageMagick-6.4.3 -rw-r--r-- 1 root root 1358 Oct 8 20:07 Mcrt1.o -rw-r--r-- 1 root root 3383 May 29 2013 PackageKitDbusTest.py -rw-r--r-- 1 root root 4267 Oct 8 20:07 Scrt1.o …
"-a" will list all files and folders, even hidden ones.
NB: Files, directories starting with "." character are treated as hidden files.
$ pwd /work/shared/course2015/intro/bash $ ls -l total 0 $ ls -la total 33 drwxr-xr-x 2 lorand hpcadmin 24576 Oct 27 10:10 . drwxr-xr-x 3 lorand hpcadmin 4096 Oct 27 10:07 .. -rw-r--r-- 1 lorand hpcadmin 1177 Oct 27 10:10 .bashrc -rw-r--r-- 1 lorand hpcadmin 849 Oct 27 10:10 .vimrc
Filename expansion is also supported with the usage of special characters, so called wildcards "?" or "*". "?" replaces one character, while "*" replaces any number of characters.
$ ls -a .*rc .bashrc .vimrc
"-t" option sorts listing by modification time, while "-r" can reverse sorting order.
$ ls -lt total 8 drwxr-xr-x 3 lorand hpcadmin 4096 Oct 27 10:07 intro drwxr-xr-x 4 floan hpcadmin 4096 Oct 21 12:10 openMP $ ls -ltr total 8 drwxr-xr-x 4 floan hpcadmin 4096 Oct 21 12:10 openMP drwxr-xr-x 3 lorand hpcadmin 4096 Oct 27 10:07 intro
cp
cp command copies files and directories. Copies source (might be one or more files/directories) to destination. When multiple files/directories are specified as source, the destination has to be a directory.
$ # copy messages file to /backup folder $ cp /var/log/messages /backup $ # copy messages and maillog files to /backup folder $ cp /var/log/messages /var/log/maillog /backup $ # copy all C source to network mounted home folder $ # preserving timestamps and permissions, be verbose $ cp -a -v ~/development/my_project/*.c /mnt/nfs/projects/sources/ $ # copy all new or modified files to destination $ cp -u -v ~/development/my_project/*.c /mnt/nfs/projects/sources/ $ # copy all new or modified files to destination $ # be verbose and interactive - ask what to do $ cp -u -i -v ~/development/my_project/*.c /mnt/nfs/projects/sources/
mv
mv moves source (might be one or more files/directories) to destination. Same as for cp command, when multiple files/directories are specified as source, the destination has to be a directory. mv command is also used for renaming a file. NB: Irreversible!
$ # interactively move python files to python_files/ subdirectory $ mv -i *.py python_files/ $ # move python files and force overwrite if similar file exists $ # be verbose $ mv -f -v *.py python_files/
rm
rm command removes files and directories. rm will not remove directories by default unless recursive - -r - option is specified. NB: Irreversible! It is advisable to create an alias for rm command to "rm -i" (see alias command).
$ # interactively remove all object files in current directory $ rm -i ./*.o $ # force removal of ~/tmp/ directory and all of it's contents $ rm -rf ~/tmp/
find
scp
rsync
cat
cat command concatenates files and prints on the standard output.
$ # concatenate motd $ cat /etc/motd Rocks Compute Node Rocks 6.1 (Emerald Boa) Profile built 16:14 10-Oct-2015
less
less allows paging trough text, one screenful at a time. Allows forward and backward movements in the text, line by line, or page by page. Useful commands in less are:
- "/" searches forward
- "?" searches backward
- "-I" switching between case-sensitive/case-insensitive searches
- "q" quits less
grep
tail
tail outputs the last part (by default last 10 lines) of the file.
$ # show last 25 lines from messages file $ tail -25 /var/log/messages $ # follow file output, output will continue until it is $ # broke with Ctrl+C $ tail -f /var/log/messages
head
awk
sed
alias
alias command defines or lists aliases. Command itself without any arguments will list existing aliases.
$ lc -bash: lc: command not found $ alias lc='ls -l --color' $ lc -rwxr-xr-x 1 root root 5008747 Oct 13 13:35 helloworld_c -rw------- 1 lorand hpcadmin 0 Oct 13 13:36 helloworld_c.err -rw------- 1 lorand hpcadmin 2068 Oct 13 13:38 helloworld_c.out $ alias alias ..='cd ..' alias ...='cd ../..' alias build='cd /work/$USER/build' alias cd..='cd ..' alias dir='ls -l' alias lc='ls -l --color'