Command Line Cheat Sheet

Browsing

ls cd mkdir rm cp mv Tab Key less

Access Rights

chmodsetfacl

Help

man-h | --help

Disk Usage

du

Session Utilities

tmuxnohupCtrl+CCtrl+Zjobsfgbgpskill

Network Utilities

rsync

Archiving

tar

Executable

chmod (execute bit)Execute Script

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 DIR

Change current location to a directory

$ ls
file1       file2   proj1   proj2
$ cd proj1
$ ls
proj1_file1 proj1_file2

mkdir

mkdir [-p] DIR

Create 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] FILE

remove files or directories

$ ls
file1  file2  proj1  proj2
$ rm file1
$ ls
file2  proj1  proj2
$ rm -r proj1
$ ls
file2  proj2

cp

cp SRC DEST

Copy a file or directory.

cp -Rt DIR SRC...

Copy files and/or directories to a directory.

mv

mv SRC DEST

Move 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 FILE

Visualize text in a pager rather than print it in the console. Use q to quit.

less -r +F FILE

Scroll 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] FILE

Set 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. --default sets 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 perms is one or more letters from the set rwxX

[g:]gid:perms:

Set group mode bits where perms is one or more letters from the set rwxX

o:perms:

Set other mode bits where perms is one or more letters from the set rwxX

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 COMMAND

Open 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) | less

Useful 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.

tmux

Open a new window

tmux ls|list

List sessions

tmux attach

Attach to the last detached window

tmux attach -t SESSION_INDEX

Attach 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+C

Interrupt the current command

Ctrl+Z

Ctrl+Z

Stop (pause) and background the current command

jobs

jobs

List the background jobs

$ jobs
[1]-  Stopped                 command1
[2]+  Stopped                 command2

fg

fg

Resume the job that’s next in the queue

bg

bg

Push the next job in the queue into the background

ps

ps -fju $USER --forest

Display 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_INDEX

Kill a job using the job’s index

kill PID

Kill a process using the process’s id

kill -- -PGID

Kill all process belonging to the process group id

$ kill %1
[1]+  Stopped                 command1

Network Utilities

rsync

rsync -arLv SRC [SRC ...] DEST

Recursively 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 SRC path 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.index

Create 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.index

Generate an index file from an existing .tar archive

tar -xf TAR_NAME.tar -C DIR

Extract 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.sh

Add the execute mode bit to a script file so it can be executed

Execute Script

./script.sh

Execute a script