Skip to content
Mingtao edited this page Nov 10, 2020 · 42 revisions
Command Description
Ctrl+a Go to start of a line
Ctrl+e Go to end of a line
Ctrl+k Delete rest of line
Ctrl+w Delete a word
Ctrl+u Delete a line
sudo -i Become root user
mv a.txt b txt rename a.txt to b.txt
mkdir -p a/b Create subdirectory when folder a doesn't exist
lpr a.txt Print out a.txt
lpstat -p Check available printers
head -5 a.txt Show first 5 lines of the file
tail -5 a.txt Show last 5 lines of the file
sort a.txt Sort the file
uniq a.txt Remove duplicate line of the file
tar -cvf a.tar folder Archive into a tar file
tar -xvzf a.tar.gz Extract tar.gz or tgz file
locate *.java File file on local system
ln -s source dest Create a soft link from source to dest hard link vs soft link
touch a.txt Create empty file, if a.txt exists, change its last modified date
cat a.txt | wc -w Count the words in the file
cat a.txt | wc -l Count the lines in the file
diff -u a.txt b.txt Check difference between two files
ls -ail list all files (including hidden files) (a), inode (i), and permission (l)
rm -rf folder delete folder and all its contents
sudo chown newowner file change owner
sudo chmod 755 file change permission 775 explain
groups which group you belong to
whoami current user
echo "hello world" > a.txt easiest way to create a file
echo "hello world" >> a.txt Append contents to a file
tail -f a.txt & Run the command in background
source ~/.bash_profile refresh the bash_profile after editing it
history | tail list recent command history
export PS1='\w: ' Change prompt to full path
export CDPATH=.::/project Define the base directory
cd - swap between current and previous directory
ps -ef | grep ndp Find processes that contain 'ndp'
ps aux | grep ndp Find processes that contain 'ndp' ?
find . -type f -size +30M -exec ls -lh {} + List files larger than 30M with human readable size
du -sh /var check size of the folder
cat /not_exist_file 2> FILE Redirect STERR to file
cat /not_exist_file &> FILE Redirect both STDOUT and STERR to file
cat /exist_file /not_exist_file |& grep "a" Redirect both STDOUT and STERR through a pipe
echo '10.9.8.12' | sudo tee -a /etc/hosts Append contents to a file requiring sudo privilege

Simple SCP example

scp msun@hammer:banjo-dev.sql .

Copy from remote server to local current folder (recursive and forceful)

scp -rf msun@hammer:/export/home/msun/ .

Find all java files under current directory

find . -name *.java

Find .svn directory (-type d), then remove them and all their contents. Not sure what {} \ is for

find . -type d -name .svn -exec rm -rf {} \;

Find msun directory (-type d), search text 'sunmingtao' recursively (-r) in the msun folder

find . -type d -name msun -exec grep -r 'sunmingtao' {} \;

search text 'SUNMINGTAO' (case insensitive -i) in folder msun recursively (-r)

grep -ir 'SUNMINGTAO' msun

Find the process running on port 8080 and kill it

lsof -i:8080
kill <pid>

Replace text - sed

#Replace first occurrence of 'abc' with ?
echo abcdeabc | sed "s/abc/?/" # print ?deabc
#Replace all occurrences of 'abc' with ?
echo abcdeabc | sed "s/abc/?/g" # print ?de?

Execute the code first in Back quote `

a=echo abc # command not found error
a=`echo abc`
echo $a # print abc

If statement

if [ $RUNENV = "local" ]
then
    echo 'local'
else
    echo 'non local'
fi

Command line arguments

#./run.sh arg1 arg2 arg3
a1 = $1 # a1=arg1
a2 = $2 # a2=arg2
a3 = $3 # a3=arg3
params=$@ # params=arg1 arg2 arg3 
number_of_argument = $# # number_of_argument=3

Go to the previous folder with pushd and popd

pushd 1
cd 2
cd 3
popd #back to original folder where pushd 1 was executed

ssh server without password

ssh-keygen # hit enter when prompting for passphrase
ssh-copy-id hammer
Clone this wiki locally