0% found this document useful (0 votes)
360 views

Bash Cheat Sheet

The document provides a cheat sheet of common Linux Bash shell commands for basic file manipulation, process management, user account creation and modification, flow redirection, archiving and compressing data, and installing software. It lists and describes commands for navigating directories, viewing and editing files, searching for files and text, sorting and filtering output, scheduling tasks, and more. The cheat sheet is intended to work with most Linux distributions and provide a quick reference to essential Bash shell commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
360 views

Bash Cheat Sheet

The document provides a cheat sheet of common Linux Bash shell commands for basic file manipulation, process management, user account creation and modification, flow redirection, archiving and compressing data, and installing software. It lists and describes commands for navigating directories, viewing and editing files, searching for files and text, sorting and filtering output, scheduling tasks, and more. The cheat sheet is intended to work with most Linux distributions and provide a quick reference to essential Bash shell commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

freeworld.posterous.

com

Linux Bash Shell


Cheat Sheet
(works with about every distribution, except for apt-get which is Ubuntu/Debian exclusive)

Legend:
Everything in <> is to be replaced, ex: <fileName> --> iLovePeanuts.txt
Don't include the '=' in your commands
'..' means that more than one file can be affected with only one command ex: rm
file.txt file2.txt movie.mov .. ..

Linux Bash Shell Cheat Sheet


Basic Commands
Basic Terminal Shortcuts

Basic file manipulation

CTRL L = Clear the terminal


CTRL D = Logout
SHIFT Page Up/Down = Go up/down the terminal
CTRL A = Cursor to start of line
CTRL E = Cursor the end of line
CTRL U = Delete left of the cursor
CTRL K = Delete right of the cursor
CTRL W = Delete word on the left
CTRL Y = Paste (after CTRL U,K or W)
TAB = auto completion of file or command
CTRL R = reverse search history
!! = repeat last command

cat <fileName> = show content of file


(less, more)
head = from the top
-n <#oflines> <fileName>

CTRL Z = stops the current command (resume with fg in foreground or bg in background)

cp image.jpg newimage.jpg = copy and rename a file

Basic Terminal Navigation

cp
cp
cp
cp

image.jpg <folderName>/ = copy to folder


image.jpg folder/sameImageNewName.jpg
-R stuff otherStuff = copy and rename a folder
*.txt stuff/ = copy all of *<file type> to folder

mv
mv
mv
mv
mv

file.txt Documents/ = move file to a folder


<folderName> <folderName2> = move folder in folder
filename.txt filename2.txt = rename file
<fileName> stuff/newfileName
<folderName>/ .. = move folder up in hierarchy

rm
rm
rm
rm

<fileName> .. = delete file (s)


-i <fileName> .. = ask for confirmation each file
-f <fileName> = force deletion of a file
-r <foldername>/ = delete folder

ls
ls
ls
ls
ls

-a = list all files and folders


<folderName> = list files in folder
-lh = Detailed list, Human readable
-l *.jpg = list jpeg files only
-lh <fileName> = Result for file only

cd <folderName> = change directory


if folder name has spaces use
cd / = go to root
cd .. = go up one folder, tip: ../../../
du -h: Disk usage of folders, human readable
du -ah:

files & folders, Human readable


du -sh: only show disc usage of folders

tail = from the bottom


-n <#oflines> <fileName>
mkdir = create new folder
mkdir myStuff ..
mkdir myStuff/pictures/ ..

touch <fileName> = create or update a file


pwd = print working directory
man <command> = shows manual (RTFM)

ln file1 file2 = physical link


ln -s file1 file2 = symbolic link

Linux Bash Shell Cheat Sheet


Basic Commands
Researching Files

Extract, sort and filter data

The slow method (sometimes very slow):

grep <someText> <fileName> = search for text in file


-i = Doesn't consider uppercase words
-I = exclude binary files
grep -r <text> <folderName>/ = search for file names
with occurrence of the text

locate <text> = search the content of all the files


locate <fileName> = search for a file
sudo updatedb = update database of files
find
find
find
find

= the
-name
-name
-name

best file search tool (fast)


<fileName>
text = search for files who start with the word text
*text =

end

Advanced Search:
Search from file Size (in ~)
find ~ -size +10M = search files bigger than.. (M,K,G)
Search from last access
find -name <filetype> -atime -5
('-' = less than, '+' = more than and nothing = exactly)
Search only files or directorys
find -type d --> ex: find /var/log -name "syslog" -type d
find -type f = files
More info: man find, man locate

With regular expressions:


grep
with
grep
grep
with

-E ^<text> <fileName> = search start of lines


the word text
-E <0-4> <fileName> =shows lines containing numbers 0-4
-E <a-zA-Z> <fileName> = retrieve all lines
alphabetical letters

sort
sort
sort
sort
sort
sort

= sort the content of files


<fileName> = sort alphabetically
-o <file> <outputFile> = write result to a file
-r <fileName> = sort in reverse
-R <fileName> = sort randomly
-n <fileName> = sort numbers

wc = word count
wc <fileName> = nbr of line, nbr of words, byte size
-l (lines), -w (words), -c (byte size), -m
(number of characters)
cut = cut a part of a file
-c --> ex: cut -c 2-5 names.txt
(cut the characters 2 to 5 of each line)
-d (delimiter)
(-d & -f good for .csv files)
-f (# of field to cut)
more info: man cut, man sort, man grep

Linux Bash Shell Cheat Sheet


Basic Commands
Time settings

(continued)

date = view & modify time (on your computer)

crontab = execute a command regularly


-e = modify the crontab
-l = view current crontab
-r = delete you crontab
In crontab the syntax is
<Minutes> <Hours> <Day of month> <Day of week (0-6,
0 = Sunday)> <COMMAND>

View:
date +%H --> If it's 9 am, then it will show 09
date +%H:%M:%Ss = (hours, minutes, seconds)
%Y = years
Modify:
MMDDhhmmYYYY
Month | Day | Hours | Minutes | Year
sudo date 031423421997 = March 14 th 1997, 23:42

Execute programs at another time


use 'at' to execute programs in the future
Step 1, write in the terminal: at <timeOfExecution> ENTER
ex --> at 16:45 or at 13:43 7/23/11 (to be more precise)
or after a certain delay:
at now +5 minutes (hours, days, weeks, months, years)
Step 2: <ENTER COMMAND> ENTER
repeat step 2 as many times you need
Step 3: CTRL D to close input

ex, create the file movies.txt every day at 15:47:


47 15 * * * touch /home/bob/movies.txt
* * * * * --> every minute
at 5:30 in the morning, from the 1st to 15th each month:
30 5 1-15 * *
at midnight on Mondays, Wednesdays and Thursdays:
0 0 * * 1,3,4
every two hours:
0 */2 * * *
every 10 minutes Monday to Friday:
*/10 * * * 1-5

Execute programs in the background


Add a '&' at the end of a command
ex --> cp bigMovieFile.mp4 &

atq = show a list of jobs waiting to be executed


atrm = delete a job n<x>
ex (delete job #42) --> atrm 42
sleep = pause between commands
with ';' you can chain commands, ex: touch file; rm file
you can make a pause between commands (minutes, hours, days)
ex --> touch file; sleep 10; rm file <-- 10 seconds

nohup: ignores the HUP signal when closing the console


(process will still run if the terminal is closed)
ex --> nohup cp bigMovieFile.mp4
jobs = know what is running in the background
fg = put a background process to foreground
ex: fg (process 1), f%2 (process 2) f%3, ...

Linux Bash Shell Cheat Sheet


Basic Commands
Process Management

Create and modify user accounts

w = who is logged on and what they are doing

sudo adduser bob = root creates new user


sudo passwd <AccountName> = change a user's password
sudo deluser <AccountName> = delete an account

tload = graphic representation of system load average


(quit with CTRL C)
ps = Static process list
-ef --> ex: ps -ef | less
-ejH --> show process hierarchy
-u --> process's from current user
top = Dynamic process list
While in top:

q to close top
h to show the help
k to kill a process

CTRL C to top a current terminal process


kill = kill a process
You need the PID # of the process
ps -u <AccountName> | grep <Application>
Then
kill <PID> .. .. ..
kill -9 <PID> = violent kill
killall = kill multiple process's
ex --> killall locate
extras:
sudo halt <-- to close computer
sudo reboot <-- to reboot

addgroup friends = create a new user group


delgroup friends = delete a user group
usermod -g friends <Account> = add user to a group
usermod -g bob boby = change account name
usermod -aG friends bob = add groups to a user without loosing the ones he's already in

File Permissions
chown = change the owner of a file
ex --> chown bob hello.txt
chown user:bob report.txt = changes the user owning
report.txt to 'user' and the group owning it to 'bob'
-R = recursively affect all the sub folders
ex --> chown -R bob:bob /home/Daniel
chmod =
u
g
o

modify user access/permission simple way


= user
= group
= other

d = directory (if element is a directory)


l = link (if element is a file link)
r = read (read permissions)
w = write (write permissions)
x = eXecute (only useful for scripts and
programs)

Linux Bash Shell Cheat Sheet


Basic Commands
File Permissions (continued)

Flow Redirection (continued)

'+' means add a right


'-' means delete a right
'=' means affect a right

terminal output:
Alex
Cinema
Code
Game
Ubuntu

ex --> chmod g+w someFile.txt


(add to current group the right to modify someFile.txt)
more info: man chmod

Another example --> wc -m << END

Flow redirection

Chain commands

Redirect results of commands:

'|' at the end of a command to enter another one


ex --> du | sort -nr | less

'>' at the end of a command to redirect the result to a file


ex --> ps -ejH > process.txt
'>>' to redirect the result to the end of a file

Archive and compress data


Archive and compress data the long way:

Redirect errors:
'2>' at the end of the command to redirect the result to a file
ex --> cut -d , -f 1 file.csv > file 2> errors.log
'2>&1' to redirect the errors the same way as the standard output
Read progressively from the keyboard
<Command> << <wordToTerminateInput>
ex --> sort << END <-- This can be anything you want
>
>
>
>
>
>
>

Hello
Alex
Cinema
Game
Code
Ubuntu
END

Step 1, put all the files you want to compress in


the same folder: ex --> mv *.txt folder/
Step 2, Create the tar file:
tar -cvf my_archive.tar folder/
-c : creates a .tar archive
-v : tells you what is happening (verbose)
-f : assembles the archive into one file
Step 3.1, create gzip file (most current):
gzip my_archive.tar
to decompress: gunzip my_archive.tar.gz
Step 3.2, or create a bzip2 file (more powerful but slow):
bzip2 my_archive.tar
to decompress: bunzip2 my_archive.tar.bz2

Linux Bash Shell Cheat Sheet


Basic Commands
Archive and compress data (continued)

Installing software

step 4, to decompress the .tar file:


tar -xvf archive.tar archive.tar

When software is available in the repositories:


sudo apt-get install <nameOfSoftware>
ex--> sudo apt-get install aptitude

Archive and compress data the fast way:


gzip: tar -zcvf my_archive.tar.gz folder/
decompress: tar -zcvf my_archive.tar.gz Documents/
bzip2: tar -jcvf my_archive.tar.gz folder/
decompress: tar -jxvf archive.tar.bz2 Documents/
Show the content of .tar, .gz or .bz2

If you download it from the Internets in .gz format


(or bz2) - Compiling from source
Step 1, create a folder to place the file:
mkdir /home/username/src <-- then cd to it
Step 2, with 'ls' verify that the file is there
(if not, mv ../file.tar.gz /home/username/src/)

without decompressing it:

gzip:
gzip -ztf archive.tar.gz
bzip2:
bzip2 -jtf archive.tar.bz2
tar:
tar -tf archive.tar
tar extra:
tar -rvf archive.tar file.txt = add a file to the .tar
You can also directly compress a single file and view the file
without decompressing:

Step 3, decompress the file (if .zip: unzip <file>)


<-Step 4, use 'ls', you should see a new directory
Step 5, cd to the new directory
Step 6.1, use ls to verify you have an INSTALL file,
then: more INSTALL
If you don't have an INSTALL file:
Step 6.2, execute ./configure <-- creates a makefile
Step 6.2.1, run make <-- builds application binaries
Step 6.2.2 : switch to root --> su
Step 6.2.3 : make install <-- installs the software
Step 7, read the readme file

Step 1, use gzip or bzip2 to compress the file:


gzip numbers.txt
Step 2, view the file without decompressing it:
zcat = view the entire file in the console (same as cat)
zmore = view one screen at a time the content of the file (same as more)
zless = view one line of the file at a time (same as less)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy