Bash short commands
From Initq
Contents |
cd
Change the current working directory to directory specified. If directory not specified then change to user's home directory.
cd - takes you back to the last directory you were at.
[alibaba@ohnonono ~]$ cd /var/www/html [alibaba@ohnonono html]$ cd [alibaba@ohnonono ~]$ pwd /home/alibaba [alibaba@ohnonono ~]$ cd - /var/www/html [alibaba@ohnonono html]$
single dot directory is your current directory and the double dot directory is one folder below.
[alibaba@ohnonono html]$ ls -la total 16 drwxr-xr-x 2 root root 4096 Aug 6 2007 . drwxr-xr-x 8 root root 4096 Dec 2 03:31 ..
you can go back or forward in one command. you can also go back to your home directory two ways.
[alibaba@ohnonono html]$ cd ../../../ [alibaba@ohnonono /]$ pwd / [alibaba@ohnonono /]$ cd ~ [alibaba@ohnonono ~]$ pwd /home/alibaba [alibaba@ohnonono ~]$ cd [alibaba@ohnonono ~]$ pwd /home/alibaba
If you are restricted to one shell and find it hard to move around directories then use the following commands:
- dirs -l (shows you what you have in your directory queue)
- dirs -v (shows entries with numbers)
- pushd /etc (will change to /etc and also queue the directory)
- popd (will pop the previous directory you were in and change to it)
If you have pushd a lot of dirs and what to get to a specific one then you can do:
- cd `dirs +3`
The list of dirs starts from the left and starts with the number 0.
echo
The echo command echos the text you entered.
env
Displays your Unix environment variables. You can type env to get a list of all your environment variables. Other environment variables include:
1. USERNAME
2. HOSTNAME
3. LOGNAME
4. MAIL
5. EDITOR - Specifies the editor to be used by default for some commands such as edquota.
Usually it is set to vi or emacs with a command like "export EDITOR=emacs".
6. TERM - The type of terminal being used.
7. PATH - The path the system looks in to find commands that the user has entered.
8. HOME - The current user's home directory
9. SHELL - The current shell program that is being executed
10. USER - The name of the current user.
11. TMPDIR - Allows programs that use the tempnam(3) function call to use the directory specified
by this variable rather than the /tmp directory.
12. SHLVL - Shows how many shells the user has invoked.
SHLVL indicates how many shells deep the user is.
[alibaba@ohnonono etc]$ echo $SHLVL 1
exec
The exec command runs an external program that you specify. It opens a new shell and runs your process. Upon completion it terminated the new shell.
exit
exit and logout commands both terminate the shell. You may also use CTRL-D.
expand
expand is a command in the UNIX Operating System. It is used to convert groups of tabs into space characters. You may need this for any program that is particular about tabs and you need to quickly convert tabs to spaces.
For example:
$ echo -e "\t foo" | expand | od -c 0000000 f o o \n 0000015 $ echo -e "\t foo" | od -c 0000000 \t f o o \n 0000006
Here the echo command prints a string of text that includes a tab character, then the output is directed into the expand command. The resulting output is then displayed by the octal dump command od. At the second prompt, the same echo output is sent directly through the od command. As can be seen by comparing the two, the expand program converts the tab (printed as '\t') into spaces.
export
Set an environment variable. Mark each name to be passed to child processes in the environment.
PATH=$PATH:/data/myscripts export PATH
effectively puts those 2 lines of code in the bash program. So within bash the $PATH variable includes /data/myscript.sh, and because of the export statement, any programs called by bash have the altered $PATH variable.
history
Bash keeps a history of commands in a file called .bash_history unless a variable called HISTFILE is defined. If you are in a tty or ssh'd in then your .bash_history file will not get written until you log out but you can still access your history with the history command.
- history (will give you full history)
- history 10 (show only last 10 history entries)
- !<num> (run the command indicated by the number)
- history -c (clear history)
- history -w (write the history to .bash_history file)
- history -a (append to .bash_history file)
join
join is used to join two files line by lines. By default it uses the first field to make a match but you can change this field with -1 and -2 flags. If you turn of case sensitivity by -i.
join -1 3 -2 2 -i people.txt numbers.txt
This command will try to match the third field of file people.txt with the second field of file numbers.txt without any case sensitivity and write the output on the screen with the matching field first, then the corresponding fields from file people.txt and then the corresponding fields from numbers.txt file.
logout
logout and exit commands both terminate the shell. You may also use CTRL-D.
mv
Move or rename files or directory.
mv myfile mynewfilename renames a file mv myfile otherfilename renames a file and deletes the existing file "otherfilename" mv myfile /myfile moves 'myfile' from the current directory to the root directory mv myfile dir/myfile moves 'myfile' to 'dir/myfile' relative to the current directory mv myfile dir same as the previous command (the filename is implied to be the same) mv myfile dir/myfile2 moves 'myfile' to dir and renames it to 'myfile2' mv foo bar baz dir moves multiple files to directory dir mv --help shows a very concise help about the syntax of the command man mv prints an extensive user manual for 'mv' in the terminal
In all cases, the file or files being moved or renamed can be a directory. If moving to a new directory, then make sure that the directory exists. There is no flag to create the directory if it does not exist.
od
od is an octal dumping program for Unix and Unix-like systems. It can also dump hexadecimal or decimal data.
od is one of the earliest Unix programs, having appeared in version 1 AT&T Unix. It is also specified in the POSIX standards (see external link below). The implementation for od used on Linux systems is usually provided by GNU Core Utilities.
[edit] Example session
Normally a dump of executable data is very long. The head program prints out the first few lines of the output. Here is an example of a dump of the ls program, piped through head.
[alibaba@ohnonono junk]$ od -x /bin/ls | head -5 0000000 457f 464c 0101 0001 0000 0000 0000 0000 0000020 0002 0003 0001 0000 9a30 0804 0034 0000 0000040 68a0 0001 0000 0000 0034 0020 0008 0028 0000060 001f 001e 0006 0000 0034 0000 8034 0804 0000100 8034 0804 0100 0000 0100 0000 0005 0000 [alibaba@ohnonono junk]$ od -d /bin/ls | head -5 0000000 17791 17996 257 1 0 0 0 0 0000020 2 3 1 0 39472 2052 52 0 0000040 26784 1 0 0 52 32 8 40 0000060 31 30 6 0 52 0 32820 2052 0000100 32820 2052 256 0 256 0 5 0 [alibaba@ohnonono junk]$ od /bin/ls | head -5 0000000 042577 043114 000401 000001 000000 000000 000000 000000 0000020 000002 000003 000001 000000 115060 004004 000064 000000 0000040 064240 000001 000000 000000 000064 000040 000010 000050 0000060 000037 000036 000006 000000 000064 000000 100064 004004 0000100 100064 004004 000400 000000 000400 000000 000005 000000
paste
paste is a Unix command line utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output. It is effectively the horizontal equivalent to the utility cat command which operates on the vertical plane of two or more files.
To paste several columns of data together into the file www from files who, where, and when:
paste who where when > www
If the files contain:
| who | where | when |
|---|---|---|
|
Sam |
Detroit |
January 3 |
This creates the file named www containing:
Sam Detroit January 3 Dave Edgewood February 4 Sue Tampa March 19
pwd
pwd stands for (print working directory) and is used to print the current directory a shell user resides in. If the shell prompt does not she the current working directory then the user can type pwd and find its place.
[alibaba@ohnonono ~]$ pwd /home/alibaba
- pwd -P (show physical directory)
- pwd -L (show logical directory)
set
set displays many options relating to bash.
[alibaba@ohnonono ~]$ set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="1" [2]="17" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.1.17(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=104
CVS_RSH=ssh
DIRSTACK=()
EUID=501
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/home/alibaba/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/alibaba
HOSTNAME=ohnonono.com
HOSTTYPE=i686
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=24
LOGNAME=alibaba
MACHTYPE=i686-redhat-linux-gnu
MAIL=/var/spool/mail/alibaba
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/alibaba/bin
PIPESTATUS=([0]="0" [1]="0")
PPID=15202
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/alibaba
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='192.168.0.7 4753 22'
SSH_CONNECTION='192.168.0.7 4753 192.168.0.10 22'
SSH_TTY=/dev/pts/1
TERM=xterm
UID=501
USER=alibaba
_=
consoletype=pty
shopt
Bash has two editing modes. emacs or vi. You can set shell options with shopt command.
qais@qais:~$ shopt cdable_vars off cdspell off checkhash off checkwinsize on cmdhist on compat31 off dotglob off execfail off expand_aliases on extdebug off extglob on extquote on failglob off force_fignore on gnu_errfmt off histappend on histreedit off histverify off hostcomplete off huponexit off interactive_comments on lithist off login_shell on mailwarn off no_empty_cmd_completion off nocaseglob off nocasematch off nullglob off progcomp on promptvars on restricted_shell off shift_verbose off sourcepath on xpg_echo off
We are going to switch from emacs to vi as the editing mode.
qais@qais:~$ shopt -o emacs emacs on qais@qais:~$ shopt -o vi vi off qais@qais:~$ shopt -s -o vi qais@qais:~$ shopt -o vi vi on qais@qais:~$ shopt -o emacs emacs off
stty
Set teletype command. Many of these commands are used when you are using a serial port device.
qais@qais:~$ stty -a speed 38400 baud; rows 31; columns 108; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
tac
tac is an Unix command that allows you to see a file line-by-line backwards. It is named by analogy with cat.
Usage:
Usage: tac [OPTION]... [FILE]...
Write each FILE to standard output, last line first.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-b, --before attach the separator before instead of after
-r, --regex interpret the separator as a regular expression
-s, --separator=STRING use STRING as the separator instead of newline
--help display this help and exit
--version output version information and exit
time
To use the command, simply precede any command by the word time, such as:
[alibaba@ohnonono ~]$ time /usr/games/fortune | mail -s "hello qais" qais@initq.com real 0m0.017s user 0m0.001s sys 0m0.004s
When the command completes, time will report how long it took to execute the ls command in terms of user CPU time, system CPU time, and real time. The output format varies between different versions of the command, and some give additional statistics, as in this example:
[alibaba@ohnonono ~]$ time host wikipedia.org wikipedia.org has address 208.80.152.2 wikipedia.org mail is handled by 10 mchenry.wikimedia.org. wikipedia.org mail is handled by 50 lists.wikimedia.org. real 0m0.137s user 0m0.007s sys 0m0.004s
type
The type command display how a name would be interpreted as a command. Where applicable type will display the name's path. Possible command types are:
- shell built-in
- function
- alias
- hashed command
- keyword
[alibaba@ohnonono ~]$ type test test is a shell builtin [alibaba@ohnonono ~]$ type cp cp is /bin/cp [alibaba@ohnonono ~]$ type bmw -bash: type: bmw: not found [alibaba@ohnonono ~]$ type type type is a shell builtin
unexpand
unexpand is a command in the UNIX Operating System. It is used to convert groups of space characters into tabs.
$ echo " asdf sdf" | unexpand | od -c 0000000 \t \t a s d f s d f \n 0000014 $ echo " asdf sdf" | od -c 0000000 0000020 a s d f s d f \n 0000032
Here the echo command prints a string of text that includes multiple consecutive spaces, then the output is directed into the unexpand command. The resulting output is then displayed by the octal dump command od. At the second prompt, the same echo output is sent directly through the od command. As can be seen by comparing the two, the unexpand program converts sequences of eight spaces into single tabs (printed as '\t'). unexpand assumes a tab stop every eight characters, you will use -t flag to change this number.
unset
unset removes the options that are displayed or modified by the set command.
uniq
uniq is a Unix utility which, when fed a text file, outputs the file with adjacent identical lines collapsed to one. It is a kind of filter program. Typically it is used after sort. It can also output only the duplicate lines (with the -d option), or add the number of occurrences of each line (with the -c option).
An example: To see the list of lines in a file, sorted by the number of times each occurs:
sort file | uniq -c | sort -n
Using uniq like this is common when building pipelines in shell scripts. Be careful, uniq expects its input to be already sorted; sometimes sort -u is what you need.
- -u Print only lines which are not repeated in the original file
- -d Print one copy only of each repeated line in the input file.
- -c Generate an output report in default style except that each line is preceded by a count of the number of times it occurred. If this option is specified, the -u and -d options are ignored if either or both are also present.
- -i Ignore case differences when comparing lines
- -s Skips a number of characters in a line
- -w Specifies the number of characters to compare in lines, after any characters and fields have been skipped
- --help Displays a help message
- --version Displays version number on stdout and exits.
[alibaba@ohnonono junk]$ cat shakes.txt to be or not to be that is the question [alibaba@ohnonono junk]$ sort shakes.txt | uniq be is not or question that the to
Variable Assignments
qais@qais:~$ filename="hello.txt" qais@qais:~$ echo $filename hello.txt qais@qais:~$ printf "%s\n$filename\n" hello.txt qais@qais:~$ printf "%s$filename\n" hello.txt qais@qais:~$
Multiple Commands
There are 3 ways to use multiple commands. One Command at a time starting from the left.
qais@qais:~$ printf "%s\n" "First line"; printf "%s\n" "Second line" First line Second line
Two commands are seperated by && and run until one of them fails.
qais@qais:~$ date && printf "%s\n" "where is my date?" Tue Nov 3 13:46:05 CST 2009 where is my date? qais@qais:~$ date 'duck' && printf "%s\n" "where is my date?" date: invalid date `duck'
Two commands are seperated by || until both of them fail or are executed.
qais@qais:~$ date 'duck' || printf "%s\n" "where is my date?" date: invalid date `duck' where is my date?
Colon Command
The colon command does nothing at all but it can still be very helpful for comments and such.
qais@qais:/var$ printf "%sYou Big Dummy"; : printf "%s\nerased from history"; printf "%s\nI can see you\n" You Big Dummy I can see you