Skip to content

Linux commands

kftsehk edited this page Aug 19, 2018 · 1 revision

A non-exhaustive list of common linux command you will likely need

File and directory

Command Description
pwd Return path to current directory.
ls List directories and files here.
ls dir List directories and files in a directory.
ls -d */ List the name of all subdirectory.
ls -a List all files including hidden files.
ls -lh List including more data in readable format.
cd dir Change directory.
cd Go to home directory.
pushd dir Save current directory (push onto stack) and go to dir.
popd dir Return (pop from the stack) to last saved directory.
dirs List all saved directories (the stack).
touch file Create an empty file.
mkdir dir Create an empty directory.
ln -s file Create a soft link here to file/dir
mv dir1 dir2 Move or renames a file.
cp file1 file2 Copy file1 to file2.
cp -r dir1 dir2 Copy dir1 to dir2 including subdirectories.
rmdir dir Remove an empty directory.
rm file Remove a file.
rm -r dir Remove a directory and its subdirectories and files.
rm -f file Remove a file, suppress all warning.
find dir -name pattern Search for file name matching pattern in dir.

Text viewing

Command Description
less file View a file.
less -N file View file with line numbers.
less -S file View file, wrap long lines.
cat file Print file to STDOUT.
tac file Print file to STDOUT in reverse line order.
head file Print first lines from a file.
head -n 5 file Print first 5 lines from a file.
tail file Print last lines from a file.
tail -n 5 file Print last 5 lines from a file.
grep str file Display lines containing str in file.
grep -c 'pattern' file Count lines matching a pattern.
sort file Sort lines from a file.
sort -u file Sort and return unique lines.
uniq -c file Filter adjacent repeated lines.
wc file Count file for line, word and characters.
wc -l file Count number of line for file.
diff file1 file2 Show difference between file1 and file2.
cut -f 1,3 file Retrieve data from 1,3 columns in a tab-delimited file.

Text editing

Command Description
paste file1 file2 Join file1 and file2 line by line.
truncate -s size file Remove contents in file.
nano -S file Nano editor with smooth scrolling.

Compression

Command Description
tar -cf file.tar dir Group files.
tar -xf file.tar Ungroup files.
tar -zcf file.tar.gz dir Group and compress files.
tar -zxf file.tar.gz Extract and ungroup files.

Remote Access

Command Description
wget url Download url.
ssh user@server SSH to a server.
scp -r local_dir user@server:remote_dir Copy file from local to remote computer.
scp -r user@server:remote_dir local_dir Copy file from remote to local computer.

IO redirection

Command Description
cmd > file Write stdout to file.
cmd >> file Append stdout to file.
cmd tee file
cmd tee -a file
cmd 2>&1 Redirect stderr to stdout.
cmd1 cmd2
cmd < file Read file as stdin.
cmd << eof-str Use multiline text as stdin, terminated by the specific sequence eof-str.
text
eof-str
cmd <<< str Use string as stdin.

File attributes

Command Description
chmod +x file Set execute permission to file.
chmod -x file Unset execute permission to file.
chmod IJK file Set permission denoted by IJK to file. I, J, K = 0 to 7, to calculate, sum all permissions, read=4, write=2, execute=1.
I is for user, J is for group, K is for everyone.
chmod -R IJK dir Set permission denoted by IJK to dir and all subdirectories and files.
chown -R user:group dir Change the ownership of dir and all subdirectories and files.

Misc

Command Description
echo string Print the string to STDOUT
printf format-str args C like printf
date Display current date time information.
time cmd Time the execution of cmd
sleep N Wait for N secs.
watch cmd Repeatedly execute cmd every 2s and display result.
which cmd Display the resolved command directory.
seq a b incr Generate a list of number starting from a to b incremented by incr.
yes Keep saying yes
yes str Keep saying str, usually used to say "no".

Useful files

Command Description
Descriptor 0 STDIN
Descriptor 1 STDOUT
Descriptor 2 STDERR
/dev/null A file that discard information
/dev/zero A file that provides 0x00
/dev/urandom A file that provide random bytes
~ Home directory
~+ Directory pointed by PWD
~- Directory pointed by OLDPWD
. Current directory
.. Parent directory
/ Root directory
~/.profile A shell-independent initialization file, not preferred.
~/.bash_profile Configure environment and preferences for login shell
~/.bashrc Configure environment and preferences for interactive shell
~/.bash_login Bash script to execute on login
~/.bash_logout Bash script to execute before logout
~/.bash_history Bash command history

Quotes in bash

Command Description
'string' Represents a string exactly as is.
"string" Represents a string exactly, except substitution and escaping
$var Replace by value of var
${var} Sometimes you need this for replacing by value of var
$(expr) Evaluate expr and substitute the result
expr Evaluate expr and substitute the result
$[arithmetic-expr] Evaluate arithmetic-expr and substitute value

Read Manual

For details of every command

Command Description
help Display bash help.
help cmd Show usage of built-in commands.
man cmd Show usage of most commands.
info cmd Show more info about command.

Others

These are useful: sed, awk, make, rsync, git, some bash or python scripting