Command
cmd --help
cmd -h
man cmd
man -k search_term
which cmd
history |tail -10
history |grep keyword
!!
!-2
File and Directory
Navigate
cd ..
cd -
pwd
xdg-open .
View
ls -l # details
ls -a # hidden
ls -F # file type
ls -t # sort by modified time
ls -r # reverse
ls -d # directory
ls -latr
tree
tree -d # directory only
ls -R # recursive
file file
du -k file # size in K
du -m file # size in M
du -h file # human readable
du -c dir # total
Modify
touch file
mkdir dir
mkdir -p a/b/c # parent directory
rmdir dir
rmdir -p a/b/c # parent directory
rm file
rm -r dir # recursive
rm -f file # force
cp src dest
cp src1 src2 dest_dir
cp -r src_dir dest_dir # recursive
mv scr dest
tar -cf file.tar dir # create
tar -tf file.tar # display
tar -xf file.tar # extract
tar -zcf file.tgz dir # zip
tar -ztf file.tgz
ln -s file link
Find
find ## all files
find dir
find file_name/pattern # current dir
find -name file_name/pattern # recursive find
find -iname File_name/pattern # ignore case
find -not -name pattern
find -ls # list
find -mtime +1 -mtime -3 # modified time in days
find -size +1M -size -5M # size
find -type d # directory
find -type f # file
find -newer file # newer
find -exec cmd {} \; # execute cmd
find -exec file {} \;
Pipeline
Redirect IO
cmd >file
cmd >>file
cmd 2>/dev/null
cmd 2>&1
cmd >file 2>&1
Utility
cut -d: -f1,2-3,4- # delimiter, fields
cut -d' ' -f2
sort
sort -k2 # key is column 2
sort -r # reverse
sort -u # unique
tr ',' ' '
tr -s ' ' '\n' # squeze space
uniq -c - count
awk '{print $1, $2}'
column -t
grep pattern file | cut -d, -f1,3 | sort | tr "," " " | column -t
ps |grep hugo | awk '{print $1'} | xargs kill
Text File
View
cat file
cat -n file # line number
tac file
less -N file # line number
head -5 file # lines
tail -5 file # lines
tail -f file # follow update
Search
grep pattern file
grep -i pattern file # ignore case
grep -n pattern file # line number
grep -c pattern file # count
grep -v pattern file # not match
cmd | grep pattern
strings binary | grep pattern
grep pattern file | less
Compare
diff file1 file2
sdiff file1 file2
vimdiff file1 file2
Executable
ps -f # details
ps -e # every proces
ps -ef
ps -u user # user's
ps -H # tree
pstree
top
cmd&
Ctrl+z # suspend
Ctrl+c # kill
jobs -l
bg %1
fg %1
kill pid
kill %jobid
kill -9 pid
Permission
User and Group
whoami
su username
useradd username -c "User" -m -s /bin/bash -g default_group
useradd svcname -c "Service" -r -d /bin/svc -s /bin/false
useradd svcname -c "Service" -u uid -d /bin/svc -s /bin/false
usermod username -aG g1,g2
userdel username
passwd username
groupadd groupname -c "Group" -g gid
groupdel groupname
groupmod groupname
groups
groups username
id
id username
tail -1 /etc/passwd
tail -1 /etc/group
File and Directory
chgrp group file
chmod 660 file
chmod u+rwx,g-w,o= file # user, group, other
chmod a+r file # all
umask
umask 0002
Directory
code |
permission |
700 |
only me |
755 |
share everyone |
750 |
group read |
770 |
group write |
775 |
share everyone, group write |
File
code |
permission |
600 |
only me |
644 |
share everyone |
640 |
group read |
660 |
group write |
664 |
share everyone, group write |
Executable
code |
permission |
700 |
only me |
750 |
share group |
755 |
share everyone |
File Creation Mask
mask |
directory |
file |
permission |
base |
777 |
666 |
|
022 |
755 |
644 |
share everyone |
002 |
775 |
664 |
share everyone, group write |
077 |
700 |
600 |
only me |
007 |
770 |
660 |
group write |
Reference
code |
bit |
permission |
7 |
111 |
rwx |
6 |
110 |
rw- |
5 |
101 |
r-x |
4 |
100 |
r– |
0 |
000 |
— |