Command Line Cheat Sheet¶
Browsing¶
ls¶
ls [-lh] [DIR]List files and directories
$ ls
file1 file2 proj1 proj2
$ ls -lh
-rw-r--r-- 1 user group 0B 4 oct 14:11 file1
-rw-r--r-- 1 user group 0B 4 oct 14:11 file2
drwxr-xr-x 2 user group 64B 4 oct 14:11 proj1
drwxr-xr-x 2 user group 64B 4 oct 14:11 proj2
$ ls -lh proj1
-rw-r--r-- 1 user group 0B 4 oct 14:14 proj1_file1
-rw-r--r-- 1 user group 0B 4 oct 14:14 proj1_file2
cd¶
cd DIRChange current location to a directory
$ ls
file1 file2 proj1 proj2
$ cd proj1
$ ls
proj1_file1 proj1_file2
mkdir¶
mkdir [-p] DIRCreate the DIRECTORY(ies), if they do not already exist
$ ls
file1 file2 proj1 proj2
$ mkdir proj3
$ mkdir -p proj4/subdir/subsubdir
$ ls -l
total 16
-rw-rw-r-- 1 user group 0 Oct 31 11:27 file1
-rw-rw-r-- 1 user group 0 Oct 31 11:27 file2
drwxrwxr-x 2 user group 4096 Oct 31 11:27 proj1
drwxrwxr-x 2 user group 4096 Oct 31 11:28 proj2
drwxrwxr-x 2 user group 4096 Oct 31 11:28 proj3
drwxrwxr-x 3 user group 4096 Oct 31 11:29 proj4
$ ls -l proj4/subdir
total 4
drwxrwxr-x 2 user group 4096 Oct 31 11:29 subsubdir
rm¶
rm [-r] FILEremove files or directories
$ ls
file1 file2 proj1 proj2
$ rm file1
$ ls
file2 proj1 proj2
$ rm -r proj1
$ ls
file2 proj2
cp¶
cp SRC DESTCopy a file or directory.
cp -Rt DIR SRC...Copy files and/or directories to a directory.
mv¶
mv SRC DESTMove or rename a file or directory.
mv -t DIR SRC...Move files and/or directories to a directory.
Tab Key¶
- Tab key
Auto-complete the text
$ cd p[tab]
proj1/ proj2/
$ cd proj
less¶
less -r FILEVisualize text in a pager rather than print it in the console. Use q to quit.
less -r +F FILEScroll forward the text of a log file and keep trying to read to update the pager as new content gets written into the file. Use Ctrl+C to interrupt the following and scroll back.
$ (for i in {1..10}; do (echo $i >> log_file.out; sleep 2) ; done) &
$ less -r +F log_file.out
[...]
~
1
2
3
4
5
6
7
8
9
10
Waiting for data... (interrupt to abort)
Access Rights¶
chmod¶
chmod MODE[,MODE] FILESet the file mode bits
MODE format¶
The format of MODE is {ugo}{+-}perms[,...], where perms is one or
more letters from the set rwxX
u:set user mode bits
g:set group mode bits
o:set other mode bits
+-:add/remove mode bits
r:read bit
w:write bit
x:execute bit
X:execute bit if already set or if the target is a directory
setfacl¶
setfacl [--default] [--recursive] {--set[-file]|--modify[-file]} MODE {DIR|FILE}Set (purge previous acl permissions) or modify file access control lists.
--set[-file]requires permissions of user, group and other to be listed.--defaultsets the default permissions to be applied to future files and sub-directory created in the directory.
MODE format¶
The format of MODE is u::perms,g::perms,o::perms[,...], where perms
is one or more letters from the set rwxX
[u:]uid:perms:Set user mode bits where
permsis one or more letters from the setrwxX[g:]gid:perms:Set group mode bits where
permsis one or more letters from the setrwxXo:perms:Set other mode bits where
permsis one or more letters from the setrwxX
r:read bit
w:write bit
x:execute bit
X:execute bit if already set or if the target is a directory
$ setfacl --default --recursive --set u:$USER:rwx,u:otheruserid:rwx dir/
$ getfacl --recursive dir/
# file: dir
# owner: ownerid
# group: groupid
user::rwx
group::rwx
other::r-x
default:user::rwx
default:user:otheruserid:rwx
default:user:userid:rwx
default:group::rwx
default:mask::rwx
default:other::r-x
# file: dir/file
# owner: ownerid
# group: groupid
user::rw-
group::rw-
other::r--
$ setfacl --recursive --modify u:$USER:rwx,u:otheruserid:rwx dir/
$ getfacl --recursive dir/
# file: dir
# owner: ownerid
# group: groupid
user::rwx
user:otheruserid:rwx
user:userid:rwx
group::rwx
mask::rwx
other::r-x
default:user::rwx
default:user:otheruserid:rwx
default:user:userid:rwx
default:group::rwx
default:mask::rwx
default:other::r-x
# file: dir/file
# owner: ownerid
# group: groupid
user::rw-
user:otheruserid:rwx
user:userid:rwx
group::rw-
mask::rwx
other::r--
Help¶
man¶
Open the help manual (man page) of a command. Not all commands have a man page entry.
man COMMANDOpen the help manual (man page) of a command.
The manual will be shown in a pager.
$ man ls
LS(1) BSD General Commands Manual LS(1)
NAME
ls -- list directory contents
SYNOPSIS
ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls displays its name as
well as any requested, associated information. For each operand that names a file of type
directory, ls displays the names of files contained within that directory, as well as any
requested, associated information.
[...]
-h | --help¶
Display help for a command. The information will be printed in the console.
command (-h|--help)- Display help for a command.Commands might have either or both options (
-h,--help). command (-h|--help) | lessUseful to scroll text in a pager rather than print it in the console
$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
[...]
$ ls --help | less
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
[...]
Disk Usage¶
du¶
du -sh [DIR]Print the disk usage of a directory
$ du -sh proj1
1.5K proj1
Session Utilities¶
tmux¶
Enables a number of terminals to be created, accessed, and controlled from a single screen.
tmuxOpen a new window
tmux ls|listList sessions
tmux attachAttach to the last detached window
tmux attach -t SESSION_INDEXAttach to a detached session
Inside a tmux terminal¶
Sessions¶
- <Ctrl+b>+s:
List sessions
- <Ctrl+b>+$:
Rename current session
Windows¶
- <Ctrl+b>+w:
List all windows
- <Ctrl+b>+c:
Create a new window
- <Ctrl+b>+d:
Detach the current window
- <Ctrl+b>+,:
Rename current window
Panes¶
- <Ctrl+b>+%:
Opens a new pane
- <Ctrl+b>+Left, Right:
Change to the left or right pane
- <Ctrl+b>+x:
Closes the current pane
nohup¶
nohup COMMAND &Run a command that will NOt HangUP when the terminal closes
Ctrl+C¶
Ctrl+CInterrupt the current command
Ctrl+Z¶
Ctrl+ZStop (pause) and background the current command
jobs¶
jobsList the background jobs
$ jobs
[1]- Stopped command1
[2]+ Stopped command2
fg¶
fgResume the job that’s next in the queue
bg¶
bgPush the next job in the queue into the background
ps¶
ps -fju $USER --forestDisplay the user’s process tree
UID PID PPID PGID SID C STIME TTY TIME CMD
user 26468 25983 25983 25983 0 10:20 ? 00:00:00 sshd: user@pts/0
user 26591 26468 26591 26591 0 10:20 pts/0 00:00:00 \_ -bash
user 32650 26591 32650 26591 0 10:44 pts/0 00:00:00 \_ ps -fju user --forest
kill¶
kill %JOB_INDEXKill a job using the job’s index
kill PIDKill a process using the process’s id
kill -- -PGIDKill all process belonging to the process group id
$ kill %1
[1]+ Stopped command1
Network Utilities¶
rsync¶
rsync -arLv SRC [SRC ...] DESTRecursively copy from source to destination, locally or remotely
Additional Options¶
- --partial
Keep partially transferred files
- --relative
Copy “implied directories” as well as the last part of
SRC. Ex.: foo/bar/ in:rsync -arLv --relative /foo/bar/baz.c ...Inserting a ./ in a
SRCpath will limit the amount of path information that is sent as implied directories. Ex.: bar/ in:rsync -arLv --relative /foo/./bar/baz.c ...- --bwlimit=RATE
Specify the maximum transfer rate for the data sent over the socket, specified in units per second. Ex.: 10 megabytes/sec bandwidth:
rsync -arLv --bwlimit=10mb REMOTE:/foo/ foo/rsync -arLv --bwlimit=10mb foo/ REMOTE:/foo/- -e <”ssh -p PORT”>
Use a non-standard SSH port
Archiving¶
tar¶
tar -cvf TAR_NAME.tar --sort=name -ML100G DIR...Create a multi-volume .tar archive (max 100G per volume) with the content of directories
tar -cvvf TAR_NAME.tar -ML100G DIR >TAR_NAME.indexCreate a multi-volume .tar archive (max 100G per volume) from a directory and save the file listing to TAR_NAME.index
tar -tvvf TAR_NAME.tar >TAR_NAME.indexGenerate an index file from an existing .tar archive
tar -xf TAR_NAME.tar -C DIRExtract a .tar archive into a directory
tar -xf TAR_NAME.tar -C DIR FILE...Extract specific files from a .tar archive into a directory
Additional Options¶
- -r
Append files to the .tar archive. This replaces
-c.- --sort=name
Sort the directory entries on name.
Executable¶
chmod (execute bit)¶
chmod +x script.shAdd the execute mode bit to a script file so it can be executed
Execute Script¶
./script.shExecute a script