Show Menu
Cheatography

Linux Command Line Cheat Sheet by

A cheat sheet of the commands I use most for Linux, with popup links to man pages.

Bash Commands

Show system and kernel
Show distri­bution
Show mounted filesy­stems
Show system date
Show uptime
Show your username
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
Output value of $NAME variable
Set $NAME to value
$PATH
Executable search path
$HOME
Home directory
$SHELL
Current shell

IO Redire­ction

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

Show current directory
Make directory dir
Change directory to dir
cd ..
Go up a directory
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

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 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
Find binary / source / manual for command
Find file (quick search of system index)

File Operations

Create file1
Concat­enate files and output
View and paginate file1
Get type of file1
Copy file1 to file2
Move file1 to file2
Delete file1
Show first 10 lines of file1
Show last 10 lines of file1
Output last lines of file1 as it changes

Watch a Command

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

Process Management

Show snapshot of processes
Show real time processes
Kill process with id pid
Kill process with name name
Kill all processes with names beginning name
 

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

Screen Shortcuts

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.

File Permis­sions

Change mode of file to 775
Recurs­ively chmod folder to 600
Change file owner to user and group to group

File Permission Numbers

First digit is owner permis­sion, second is group and third is everyone.
Calculate permission digits by adding numbers below.
4
read (r)
2
write (w)
1
execute (x)
                               
 

Comments

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 :)

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

DaveChild DaveChild, 10:02 28 Nov 11

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

DaveChild DaveChild, 10:02 28 Nov 11

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

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

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>&-

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

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.

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

Awesome job! This will definitely come in handy

download PDF is broken

Good work. Thanks, its helpful.

Hi,

Fantastic good job..

Thanks

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

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

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.

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

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

hi!.these are useful cheat sheet .

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

top
CTRL-Z
ps aux | grep top
fg

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

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

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

Nice one...really helpful

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.

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.

Missing: sed
find -exec

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

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

This is bash-tastic!

thank you very much, it is great

There is no commands for shutdown or reboot..

Good job on the cheatsheet - this will definitely come in handy for my Linux exam next week. Some commands on umask would be a good addition to this cheatsheet :).

Hi, handy sheet. Just one thing. Find will do a recursive search by default. In the find /dir -name name* should be in double quotes if you want it to recursively find all files starting with name:

find /dir/ -name "name*"

That's because, without the quotes, the shell will expand the wildcard before handing the parameters to find. To unexpected things might happen if you don't have the quotes. Please see the examples below:

$ find .
.
./name1
./name2
./dir1
./dir1/name3
./dir1/name1
./dir1/test3
./test1
$ find . -name name*
find: paths must precede expression: name2
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
$ find . -name "name*"
./name1
./name2
./dir1/name3
./dir1/name1
$ touch dir1/fred1
$ find .
.
./name1
./name2
./dir1
./dir1/name3
./dir1/name1
./dir1/test3
./dir1/fred1
./test1
$ find . -name name*
find: paths must precede expression: name2
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
$ find . -name "name*"
./name1
./name2
./dir1/name3
./dir1/name1
$ find . -name fred*
./dir1/fred1
$ find . -name "fred*"
./dir1/fred1
$

grep -B#

This shows what you are searching plus additional lines where number is added. Great for DHCP lease searching.

I would suggest to put in screen <tty.device> <baudrate> – just in case anybody needs to connect to some serial console…

Great reference!
The section on Screen is missing "Ctrl-A d" for detaching.

Great resource - thanks a lot Dave!

add task manage comman:
gnome-system-monitor

Command that might be used to test connectivity of Linux box to the Internet, get logged on user info, and get TCP/IP configuration info. Can anyone help me?

theres also a good one here: http://www.techietek.com/2014/04/29/linux-cli-cheat-sheet-wallpaper/

I've always found `mkdir -p path/to/directory` to be useful.

head -n1 /etc/issue as a means to access distribution name isn't reliable, since the post-login screen could have been customized.

A more reliable version would be sed -nr 's/^PRETTY_NAME=(.*)/\1/p' /etc/os-release . This should be true even for distros using legacy init (i.e., current Debian, Gentoo and even Slackware).

ls -h? human readable?

would be good to add Ctrl-left/right for jumping arguments, Ctrl-home/end for jumping whole line.

'echo $SHELL' actually prints the default shell, 'ps -p $$' will print the current shell

Love the colour, but could it be a slightly darker/stronger green? It comes out almost impossible to read on my colour printer. I tried outputting in greyscale, but the green-on green sections are still unreadable. Needs more contrast.

Alternatively, change all the green texts to black and keep the backgrounds green?

DaveChild DaveChild, 16:05 28 Mar 16

I agree, it's not a great colour. I've changed it.

Could you add "passwd" to the file? :)

thank you for this really useful sheet !

The download button seems to be broken though :(

NatalieMoore NatalieMoore, 05:13 10 Apr 16

Hey GloObi, thanks for the heads up about the broken download button on this cheatsheet. We'll get it fixed and I'll let you know when its been done. Have a great day.

NatalieMoore NatalieMoore, 06:49 13 Apr 16

Hi GloObi,

This is now fixed. Please let me know if you have any more problems.

Nat

Can't download. Sad.

NatalieMoore NatalieMoore, 05:07 10 Apr 16

Thanks d.falkovsky, I will let Dave know the download button on this cheat sheet needs to be fixed. I will let you know once he has fixed it :). Sorry for the inconvenience.

NatalieMoore NatalieMoore, 06:48 13 Apr 16

Hi d.falkovsky,

This is now fixed, you should now be able to download. Please let me know if you have any more problems.

Nat

Thank you, Natalie!

Good job Dave, Thank you :)

I think the colour pdf link directs back to the original page

Good for quick reference Thank you

All links for download are dead, pleas fix this issue ASAP. Would be greatly appreciated, thanks.

thank you very much Dave

thanks Dave
besides ^w my most used nano commands:
Alt+Space Go back one word
Ctrl+Space Go forward one word

check this sheet!
https://cheatography.com/sathyanarayanan/cheat-sheets/linux-command-line/

could you please add ll comand

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          mod_rewrite Cheat Sheet
          Linux, Bash, and System Administration Cheat Sheet
          Linux Every Day Commands Cheat Sheet

          More Cheat Sheets by DaveChild

          Regular Expressions Cheat Sheet
          CSS2 Cheat Sheet
          mod_rewrite Cheat Sheet