or   Register or Register today to make and share your own cheat sheets! (Why Join?)

Cheatography Login

Join Us!

Not a Cheatographer? Register here!

Social Media

You can login to or register with Cheatography using your Facebook or Twitter account!

Why Join Cheatography?

Make and share cheat sheets!
Join a great community of Cheatographers and add your very own contributions.

Save your favourites!
Quick access to your most loved cheat sheets.

Fewer ads!
Members see no ads on the site.

Coming soon ...
Requests, ratings and more!

Why Join Cheatography?

Make and share cheat sheets!
Join a great community of Cheatographers and add your very own contributions.

Save your favourites!
Quick access to your most loved cheat sheets.

Fewer ads!
Members see no ads on the site.

Coming soon ...
Requests, ratings and more!

Linux Command Line Cheat Sheet by DaveChild

32 Comments   |   Add a Comment   |   PDF Download   |   Find:

Bash Commands

uname -a Show system and kernel
head -n1 /etc/issue Show distri­bution
mount Show mounted filesy­stems
date Show system date
uptime Show uptime
whoami Show your username
man command Show manual for command

Bash Shortcuts

CTRL-c Stop current command
CTRL-z Sleep program
CTRL-a Go to start of line
CTRL-e Go to end of line
CTRL-u Cut from start of line
CTRL-k Cut to end of line
CTRL-r Search history
!! Repeat last command
!abc Run last command starting with abc
!abc:p Print last command starting with abc
!$ Last argument of previous command
ALT-. Last argument of previous command
!* All arguments of previous command
^abc­123 Run previous command, replacing abc with 123

Bash Variables

env Show enviro­nment variables
echo $NAME Output value of $NAME variable
export NAME­=v­alue Set $NAME to value
$PATH Executable search path
$HOME Home directory
$SHELL Current shell

IO Redirection

cmd < file
Input of cmd from file
cmd1 <(­cmd2)
Output of cmd2 as file input to cmd1
cmd > file
Standard output (stdout) of cmd to file
cmd > /dev/null
Discard stdout of cmd
cmd >> file
Append stdout to file
cmd 2> file
Error output (stderr) of cmd to file
cmd 1>&2
stdout to same place as stderr
cmd 2>&1
stderr to same place as stdout
cmd &> file
Every output of cmd to file

cmd refers to a command.

Pipes

cmd1 | cmd2
stdout of cmd1 to cmd2
cmd1 |& cmd2
stderr of cmd1 to cmd2

Command Lists

cmd1 ; cmd2
Run cmd1 then cmd2
cmd1 && cmd2
Run cmd2 if cmd1 is successful
cmd1 || cmd2
Run cmd2 if cmd1 is not successful
cmd &
Run cmd in a subshell
 

Directory Operations

pwd Show current directory
mkdir dir Make directory dir
cd dir Change directory to dir
cd .. Go up a directory
ls List files

ls Options

-a Show all (including hidden)
-R Recursive list
-r Reverse order
-t Sort by last modified
-S Sort by file size
-l Long listing format
-1 One file per line
-m Comma-­sep­arated output
-Q Quoted output

Search Files

grep pattern files Search for pattern in files
grep -i Case insens­itive search
grep -r Recursive search
grep -v Inverted search
grep -o Show matched part of file only
find /dir/ -name name* Find files starting with name in dir
find /dir/ -user name Find files owned by name in dir
find /dir/ -mmin num Find files modifed less than num minutes ago in dir
whereis command Find binary / source / manual for command
locate file Find file (quick search of system index)

File Operations

touch file1
Create file1
cat file1 file2
Concat­enate files and output
less file1
View and paginate file1
file file1
Get type of file1
cp file1 file2
Copy file1 to file2
mv file1 file2
Move file1 to file2
rm file1
Delete file1
head file1
Show first 10 lines of file1
tail file1
Show last 10 lines of file1
tail -f file1
Output last lines of file1 as it changes

Process Management

ps Show snapshot of processes
top Show real time processes
kill pid Kill process with id pid
pkill name Kill process with name name
killall name Kill all processes with names beginning name

Watch a Command

watch -n 5 'ntpq -p'
Issue the 'ntpq -p' command every 5 seconds and display output
 

Nano Shortcuts

Files
Ctrl-R Read file
Ctrl-O Save file
Ctrl-X Close file
Cut and Paste
ALT-A Start marking text
CTRL-K Cut marked text or line
CTRL-U Paste text
Navigate File
ALT-/ End of file
CTRL-A Beginning of line
CTRL-E End of line
CTRL-C Show line number
CTRL-_ Go to line number
Search File
CTRL-W Find
ALT-W Find next
CTRL-\ Search and replace

More nano info at:
http:/­/ww­w.n­ano­-ed­ito­r.o­rg/­doc­s.php

Screen Shortcuts

screen
Start a screen session.
screen -r
Resume a screen session.
screen -list
Show your current screen sessions.
CTRL-A
Activate commands for screen.
CTRL-A c
Create a new instance of terminal.
CTRL-A n
Go to the next instance of terminal.
CTRL-A p
Go to the previous instance of terminal.
CTRL-A "
Show current instances of terminals.
CTRL-A A
Rename the current instance.

More screen info at:
http:/­/ww­w.g­nu.o­rg­/so­ftw­are­/sc­reen/

File Permissions

chmod 775 file
Change mode of file to 775
chmod -R 600 folder
Recurs­ively chmod folder to 600
chown user­:g­roup file
Change file owner to user and group to group

File Permission Numbers

The first digit is the owner permis­sion, the second the group and the third for everyone.
Calculate each of the three permission digits by adding the numeric values of the permis­sions below.
4 read (r)
2 write (w)
1 execute (x)

Favourited by 86 Members:

nschurdell blacklisted danh gerben Pyker CITguy CyberTron lopper69 DaveChild xcjjzh greggannicott alexmail93 arnoldobr anomalophobe manoelhc CliveWalkden naiquevin brentscheidt shikee iknowsbest rexington javovo SubCat7564 loosh srb zenweasel naesk Wbbourne maintainweb gamiclea jasonlindberg devz sebbu fedesilva dezos kassius slucas Bertrand blebo Vaibhav fire9 schrodervictor guslong aaronwolen rd5coding Jay Taylor rvalerob xaled Huyderman cmdmcd2 onitek dttk simspace andshecodes2 cartoonist martinku turin13 whereisthetea petraeus mporras JamesMcAulay avalitan seeekr Huskeraider norixxx apurohit CaymanPirate PierreBdR Epoc wdfelippe lindmark akipta ovi_mihai dustan oronm papousekp mambero kayalshri ciphan PixelHippie chi_zach rajivvishwa ivanfioravanti ifdattic tuuli Dranzz

Comments

gerben gerben, 10:02 28 Nov 11

Thanks for creating this cheat-sheet Dave. The one thing I missed was "grep -o"; Show only the part of a matching line that matches PATTERN

DaveChild DaveChild, 10:02 28 Nov 11

I've added "grep -o" to the cheat sheet :)

a_statham a_statham, 10:02 28 Nov 11

The I/O redirection section could use "2>" and "&>" examples, I always forget how to redirect stderr

DaveChild DaveChild, 10:02 28 Nov 11

Good idea - I'll add that (once I remember how they work myself ... :) )

DaveChild DaveChild, 10:02 28 Nov 11

I've updated that section to include stderr redirection. :)

wattslevi wattslevi, 19:51 29 Nov 11

How are the indented lines added several of the cells like Screen Shortcuts? RE: http://getsatisfaction.com/cheatography/topics/adding_a_two_line_entry_in_a_list

DaveChild DaveChild, 08:35 30 Nov 11

Those indented bits are a "question and answer" format box.

wattslevi wattslevi, 10:30 30 Nov 11

Ah, Q&A means I would be able to cheat with it and use it for two column with an extra line. Thanks for the info.

(Replying to my original post seems unintuitive for trying to reply under your post, but not to the overall thread... assuming this post as I'm thinking it might.)

jim jim, 23:12 30 Nov 11

On the redirects, the one I most commonly use is ignoring errors (2>/dev/null, or more succinctly 2>&- ).

For example, if I'm looking for files and I don't care that I haven't access to parts of the filesystem, we might do something like:

find / -name "*.html" 2>&-

Tanner Tanner, 10:30 7 Mar 12

ls -h is handy - changes sizes to human readable formats. Goes along good with -S. If I'm using it it is generally a ls -alhS

Wane Wane, 07:28 21 Mar 12

I think the "Bash Shortcuts" part is a little misleading. The "ctrl-a", "ctrl-e", "ctrl-k" is in emacs mode. But there should be many people preferring vim-mode or some thing like that.

Davis Peng Davis Peng, 13:57 22 Mar 12

Thanks a lot for your sheet, I just need such a linux command summary such as this sheet.

gamiclea gamiclea, 00:05 2 Jun 12

Awesome job! This will definitely come in handy

wolvverine wolvverine, 11:29 6 Jun 12

download PDF is broken

Arpit Arpit, 10:51 29 Jun 12

Good work. Thanks, its helpful.

Chetan Morajkar Chetan Morajkar, 14:35 6 Aug 12

Hi,

Fantastic good job..

Thanks

Donald J. Tambeau Donald J. Tambeau, 15:23 6 Aug 12

Love your work....it is so ....so helpful! I would like to format a MicroDisk using Linux Centros. When I look at the Partition on the MicroDisk, I see the following comment....(non-Linux). This makes sense, since I formatted with a computer running Win 7! I want to use it on my Linux system but do not know how to do it. I tried format /dev/scd1 but no joy!
Thank you
Don

Eric Eric, 08:59 7 Aug 12

I'd just like to point out a trick I use with the head and tail commands:
First, you can designate the number of lines to return, like so: head -100 filename
I often use both head and tail together to get a section of a file by piping the output of one into the other. The following command gets 100 lines, starting 1000 lines before the end of the file:
tail -1000 filename | head -100
You could, of course, reverse the commands to get a section near the top of the file. To get lines 91-100:
head -100 filename | tail -10

Mark Mark, 08:53 29 Aug 12

I just wanted to take a moment to thank you for putting this together. This is a big help to me I am new to OpenFiler witch I believe uses bash at the core so I am of course new to Linux. I didn't really think it would be quite so difficult to find resources that one can use to navigate the command line but I guess most folks use the GUI. But, I'm not most folks when I bought my first IBM 8088 I started with DOS.Anyway, thanks for the time and effort you put into this, sorry about digressing there.

Shrinath Shrinath, 13:08 4 Oct 12

Excellent Stuff man.. I think these are the most commonly used commands.. Good Effort.

JImmy JImmy, 21:43 11 Oct 12

you need to figure out how to split your command sheet as a pdf

Niloufar Niloufar, 21:43 11 Oct 12

hi!.these are useful cheat sheet .

russ russ, 16:18 22 Oct 12

CTRL-Z sleeps (stops) the running process. fg [#] brings it back to foreground.

top
CTRL-Z
ps aux | grep top
fg

chiahsun chiahsun, 11:14 2 Jan 13

Show human readable format (kb, mb...)
ls -lh

PierreBdR PierreBdR, 11:35 9 Feb 13

This cheat-sheet is very good! Thanks for that.

However, I don't like the "chmod" commands you are using. I don't think anybody should use the numeric version of chmod anymore. Your example "chmod -R 600 folder", is the best way to lock yourself out of your own folder and loose any executable bits on the scripts. I would rather use the symbolic version:

chmod -R u+rw folder # Add read-write for user on all files in folder
chmod -R og-rwx # Remove read write and execute bits for "other" and "group" on all files in folder

And my all-time favorite:

chmod -R og=u-w folder # Give other and group the same rights as user, but removing writing rights.

Of course, this also handles t and s bits:

chmod u+s file
chmod o+t folder

Stylius Stylius, 09:17 15 Feb 13

I would also add the tar command. No sysadmin would survive without it.

To extract tar.gz archive
tar xvzf archive.tar.gz

To extract tar.bz2 archive
tar xvjf archive.tar.bz2

To extract tar archive
tar xvf archive.tar

To create archive
tar cvzf archive.tar.gz /file_or_folder/to/archive

Sandeep Sandeep, 08:43 16 Feb 13

Nice one...really helpful

John John, 08:43 16 Feb 13

Great resource - thanks for taking the time and trouble to put this out there.

My favorite ls options are '-ltr' . The t sorts files by time, and r reverses that, so newest files show up right above the prompt, no matter how long the listing is.

daveydave400 daveydave400, 08:43 16 Feb 13

What about CTRL+y to paste the stuff you cut back in? How can you leave that out? CTRL+a/e and CTRL+u/y are the pairs I remember.

gymka gymka, 08:44 16 Feb 13

Missing: sed
find -exec

Jared Jared, 10:33 26 Feb 13

Nice work, but it would be best as a single page PDF.

Darr247 Darr247, 16:31 5 Apr 13

Or if the PDF at least split into 2 pages (so it could be a laminated 2-sided sheet) without cutting commands in half.

Add a Comment

Comment:

Contents

A cheat sheet of the commands I use most for Linux.

Column Content Comments Author Updated
- Linux Command Line Cheat Sheet DaveChild 12 May 13
1 Bash Commands 0 DaveChild 28 Oct 11
Bash Shortcuts 0 DaveChild 21 Nov 11
Bash Variables 0 DaveChild 28 Oct 11
IO Redirection 0 DaveChild 22 Nov 11
Pipes 0 DaveChild 22 Nov 11
Command Lists 0 DaveChild 22 Nov 11
2 Directory Operations 0 DaveChild 27 Oct 11
ls Options 0 DaveChild 26 Oct 11
Search Files 0 DaveChild 22 Nov 11
File Operations 0 DaveChild 22 Nov 11
Process Management 0 DaveChild 28 Oct 11
Watch a Command 0 DaveChild 15 Feb 13
3 Nano Shortcuts 0 DaveChild 1 Feb 12
Screen Shortcuts 0 DaveChild 22 Nov 11
File Permissions 0 DaveChild 22 Nov 11
File Permission Numbers 0 DaveChild 30 Oct 11