Bash: Useful Commands
Published: Aug 19, 2017
Tags: shell
Category: Engineering
Bash is a widely shell, which is the default shell in many operating systems, e.g. Unix, Linux, Mac OS. Bash can help you achieve a lot. To invest time to learn some useful commands in bash is a good idea.
Table of Contents
Directory commands
Home directory
For every user, there is a home directory which is presented by ~
cd will go to home directory
Handle space
There are two ways to handle space in the path:
1cd 'My Documents'
2
3cd My\ Documents
List files
ls -R: will show directory recursively
*file **: can show files with format
Others
pwd : show the working directory
rm -rf [directory]: remove a not empty directory
Wildcard
Bash supports wild card to match files.
1*
Matches anything, including nothing at all
1?
Matches exactly 1 character
1[acd7_]
Matches one of the characters in the list, above example would match a, c, d, 7 or _
Another example is: [^ax2] matches anything but a, x, 2
You can also use ranges, e.g. [a-z], [0-9], [A-C3-5]
Brace Expansion
Brace expansion is another handy way for you to write compact commands.
1touch {a,b,c}.txt => touch a.txt b.txt c.txt
2
3mv file.{txt,jpg} dir/ => mv file.txt file.jpg dir
4
5touch {a..c}{1..3}.txt => touch a1.txt a2.txt ... c2.txt c3.txt
6
7mv *{txt,jpg} Documents => mv *txt *jpg Documents
File Manipulation
Input/Output redirection
Output redirection is very important tool, which has two modes:
1> will overwrite
2>> will append
< is input redirect
cp
cp is the copy file command.
cp -R will copy files recursively
Modify file
Touch will create a empty file or update access time on an existing file
sort command can sort the content in the file.
1sort -nk2 [filename]*: can sort the content of file according to the second column
tr can replace character
1tr 'Hello' 'hello' < test > test2
cut: cut out selected positions of each line of a file
1cut -c2 test => cut the second character of each line in test file
paste: get content from various files and put into one file
1paste test test2
join: do the similar thing like paste but get rid of the header for each row
Search file
grep command will do the search and list all the relevant lines
1grep -nr security . => search keyword security in current folder
find can use pattern to find files
1find . -name "*.rst" => find all rst files under current folder
wc: count lines, words and characters in a file
uniq: do not show duplicated items
head and tail: show the beginning and end of the file
View help and file content
Use man to see the help manual
1Space: down the page
2B: up the page
3/: search. N, n to go to next/previous match
4Q: exit
Use less command to view a file, use the same keys like man
Process
If you start a long process by a normal way, the terminal will be blocked.
- Ctrl + Z will pause the process.
- fg will bring back the process.
- bg will let the process run in the background
When you start a long program, you can also ends up a &, which means run in background
jobs: see the process in background
kill can kill a process. e.g. kill %1
ps -e: list processes
The difference between jobs and ps is jobs only list the process managed by the shell.
Other tips
Commands parameters
The command option can be combined in one dash
1For example: ls -l -a
2Can be: ls -la
Avoid keeping sudo
In some operating system, if your account is not admin you will be asked to type sudo often. It is inconvenient.
sudo -s: can avoid typing sudo every time
Shortcut keys
- Ctrl-a: Start of line
- Ctrl-e: End of line
- Ctrl-Left: Forward 1 word
- Ctrl_Right: Back 1 word
- Alt-D: Delete a word
- Alt-Backspace: Delete a word backward
- Ctrl-K: Delete rest of line
- Ctrl-U: Delete from start of line
- Ctrl-R: Search back in history
Alias
alias to show the alias
alias v=gvim
\ls will use the original commands and ignore the alias
Others
Select the text and click mid mouse button will do the copy/paste the selected text in terminal
ssh allows you to login the another system
var1='hello'
echo $var1
Written by Binwei@Oslo