source (https://docs.google.com/document/d/1v5cLry8GqD6JvlsCbDf3PfLNWRZzv1nfoz8j72TRYr4/edit\)

How to Read This

  • Replace things in italics with actual filenames, URLs, etc.

  • Anything in brackets [] is optional.

  • file means one filename

  • dir means a directory name

  • name means a filename or directory name

  • file [...] means "one or more files"

  • Note: most commands can operate on multiple files, even if this sheet doesn't say so

File Names and Globbing

Used with any of the commands, below.

foo A file named foo

foo* Files whose names begin with foo

*foo Files ending with foo

foo? Files starting with foo and ending with any single letter

???? Files made up of four letter names

foo*bar Files that start with foo and end with bar

1*2*3 Files that start with 1, end with 3, and have 2 in between

"foo bar" File with a space in the name

foo.{gif,jpg} Expands tofoo.gif foo.jpg

TAB completion

Start typing a filename or command name and hit TAB to complete it. Hit two times to see options if nothing happens.

Help

mancommandShow manual page for a command

helpbuiltinShow Bash help for a built-in command, e.g. "help cd"

Exiting the Shell

exit Exit the shell

CTRL-D Send End-Of-File (EOF) to exit the shell

Getting Bearings

lsShow directory listing

ls -lShow long (detailed) directory listing

ls -aShow all (including hidden) files

ls -laShow all, long

pwdPrint Working Directory—where am I?

cdSwitch back to home directory

Permissions fromls -l

Arranged in triple of triples. Read, Write, and Execute permission for user, group, and other:

drwxrwxrwxLeadingdmeans "Directory"

drwxrwxrwxUser permissions in bold

drwxrwxrwxGroup permissions in bold

drwxrwxrwxOther permissions in bold

drwxr-xr-xDirectory. User can read, write, and enter. Group can read

and enter. Other can read and enter.

-rwxr-xr-xFile. User can read, write, and execute (it's a program).

Group can read and execute. Other can read and execute

-rw-r-----File. User can read and write. Group can read. Other can do

nothing.

See also:chmod

Moving Around and Directories

cddirnameChange directory

cd ..Change to parent directory

cd -Change to previous directory

cdChange to home directory

cd ~Change to home directory, if you're ambitious

mkdirdirnameMake a new directory

rmdirdirnameRemove an empty directory

pushddirnameChange to directory and push it on the directory stack

popdPop directory off directory stack and change to it

dirsView the directory stack

Directory Names

../fooDirectory (or file) foo out of the parent directory

./fooDirectory (or file) foo in the current directory

/The root directory of the filesystem

~My home directory

~/fooDirectory (or file) foo out of my home directory

-Previous directory (cdcommand only!)

File Manipulation

rmfileRemove (delete) a file

rm -ifileAsk for verification before delete

rm -rdirRecursively remove a directory tree.Danger!

rm -rfdirRecursively remove a directory tree, force.Danger!

ls -lfileList details about file

ls -ldirList details about files in a directory

ls -lddirList details about a directory

cpfile1file2Make a copy of file1

mvfile1file2Rename file1 to file2

mvfiledirMove file into another directory

statfilePrint metadata about file

locatepatternLocate all files with matching pattern

filefileIdentify type of file from its contents

chmod

chmodmodefileChange permissions on a file

chmod 755fileUser can RWX, group RX, other RX

chmod 644fileUser can RW, group R, other R

chmod 700fileUser can RWX

chmod 600fileUser can RW

chmod u+xfileAdd X permission to user

chmod g-rfileRemove R permission from group

chmod o+wfileAdd W permission to other

chmod a+rwfileGive all users RW permission

Note that your directories needat leastu+x (or 700) permission if you want to be able to read them yourself.

File Manipulation II

headfileShow first 10 lines of a file

head -23fileShow first 23 lines of a file

tail -n +7fileShow end of file starting from 7th line from beginning

tail -n 13fileShow last 13 lines of file

tail -ffileShow last 10 lines of file, then show more as file is updated

morefilePage through a long file

lessfilePage through a long file, improved

mostfilePage through a long time, improved more

sortfileSort a file a line at a time

wcfileWord count: lines, words, characters

wc -wfileWord count: words only

wc -lfileWord count: lines only

sort -ufileSort a file (unique), collapse duplicate lines into a single line

catfile[…]Display file(s) on the screen

cut -d' ' -f 5fileCut the 5th space-delimited field from each line

cut -d' ' -f 6,7fileCut the 6th and 7th space-delimited fields from each line

sedPowerful stream editor

sed 's/foo/bar/g'Replace all occurrences offoowithbar

awkPowerful text file processing language

ls -l | tail +2 | grep -v ^d | awk 'BEGIN {t=0} {t+=$5} END {print t}'

Add up size in bytes of all files in the current directory

grep

grep -EpatternfileUse extended regular expressions

egrepShort forgrep -E(people tend to use this instead ofgrep)

greppattern fileSearch for pattern in file

grep -vpattern fileSearch for not-pattern in file

grep -cpattern fileCount the number of times pattern appears in file

grep -ipatternfileCase-insensitive grep

grep -lpatternfileShow matching file names only ("minus ell")

grep '^Hello'fileShow all lines beginning with "Hello" in a file

grep 'Bye$'fileShow all lines ending with "Bye" in a file

find

Multiple arguments can be specified at once to, for example, find all regular files ending in .mp4 that are larger than 1000 MB.

find . -namepatternFind files from current directory matching pattern

find . -name \foo\Find files with "foo" anywhere in the name

find . -size +100MFind files larger than 100 MB

find . -type dFind files that are directories

find . -type fFind files that are regular files

find . -type f -exec grep -lipattern{} \;

Show names of files containing a pattern

Editors

vimfileRun the vim text editor

nanofileRun the nano text editor

emacsfileRun the Emacs text editor

Command History

UPorCTRL-PPrevious command in history (left/right to move cursor)

DOWNorCTRL-NNext command in history

CTRL-RtextSearch for a previous command containing text

historyLook at the command history

!numberSubstitute command number from history here

!!Substitute previous command here
!^Substitute first argument of previous command here

!$Substitute last argument of previous command here

!*Substitute all arguments of previous command here

!commandSubstitute last command beginning with given command

set -o viSet command line editing mode to vi (vim) mode

unset HISTFILEDon't save history from this bash session

Output

echotextShow text on screen, followed by a newline

printftextShow text on screen, no newline

printf "text\n"Show text on screen with a newline

Redirection

command>fileRedirect output of command into file

command<fileRedirect input of command from file

command2>fileRedirect stderr of command to file

command>file2>&1Redirect both output and stderr of command to file

command>>fileAppend output of command to a file

command2>>fileAppend standard error output to a file

Pipes

command1|command2Pipe output ofcommand1into input ofcommand2

command12>&1 |command2Pipe standard error output ofcommand1intocommand2

Networking

sshuser@hostnameSSH to a remote machine

lftpuser@hostnameLFTP to a remote machine

ftphostnameFTP (older client) to a remote machine

telnethostnameTelnet to a remote machine

lynxurlRun the Lynx text-based web browser

linksurlRun the Links text-based web browser

pinghostnameSee if a host is reachable over the network

traceroutehostnameTrace all hops a packet takes to reach a host

Process Management

CTRL-ZSuspend a running foreground job

jobsShow all jobs

fgPut last suspended job in foreground

fg %2Put a specific job in foreground

bgPut last suspended job in background

psShow all processes running in this terminal (tty)

ps -uShow all processes running for this user attached to a tty

ps -uxShow all processes for this user

killpidKill process (terminate signal)

kill -9pidKill process (kill signal—process cannot ignore)

topText GUI presentation of currently running processes

ln -sfile1file2Makefile2a symlink tofile1

ln -s/some/pathnameMakenamea symlink to a file or directory

lnfile1file2Makefile2a hard link tofile1

Users

wShow users on system

whoShow users on system (alternate)

whoamiShow your current user name

last

suuserSwitch to another user

su -Run a superuser/root shell

sudocommandRun a command as superuser

Date and Time

dateShow date and time

calShow calendar for this month

cal 7 1999Show calendar for July, 1999

cal 2015Show for 2015

System Info

unameShow OS/system info

uname -aSome more complete OS/system info

Archives, Compression

tar xvffile.tarExtract an uncompressedtararchive

tar xvffile.tar.gzExtract atar/gziparchive

tar xvffile.tgzExtract atar/gziparchive, alternate extension

tar xvffile.tar.xzExtract atar/xzarchive

tar cvffile.tarfile1[...]Create an uncompressedtararchive

tar cavffile.tgzfile1[...]Create a gzip-compressedtararchive

tar cavffile.tar.xzfile1[...]Create anxz-compressedtararchive

zip -rfile.zipfile1[...]Create a ZIP archive

unzipfile.zipExtract a ZIP archive

gzipfileCreate a compressed (.gz) version of this file

gunzipfile.gzUncompress this file

zgrep [options]file.gzgrep(see above) a Gzipped file

zmorefile.gzmore(see above) a Gzipped file

Disk Information

dfShow all mounted drives and information

dunameShow disk usage for file or directory

du -knameShow disk usage in KB

du -mnameShow disk usage in MB

du -hnameShow disk usage in a human-readable form

du -snameShow summary disk usage for a directory

du -shnameShow summary/Human-readable disk usage

Aliases

alias ls='ls -l'Make it so when you typels, it turns intols -l

alias p='ping'Addpas shorthand forping

Variables and Substitutions

Exported variables tend to be capitalized by convention.

setShow all set variables

VAR=valueSet variable to value

exportVARMark variable to be exported to subprocesses

exportVAR=valueSet variable, and mark as exported to subprocesses

$VARShow the value stored in the variable

PATH=$PATH:/some/pathAppend a path to the PATH variable

vim $(find . -name \*.txt)Open in vim all files in all subdirectories with.txtextension

read xRead from standard input into variablex

export PS1='beej$ 'Set main prompt tobeej$

Shell Initialization Scripts

~/.bash_profileContains commands to be executed on the first shell

you log in from ("login shell").

~/.bashrcContains commands to be executed in any interactive

non-login shell.

source ~/.bashrcRerun.bashrcin this shell (e.g. after you've made changes

to it).

. ~/.bashrcShorthand forsource.

Often people just use their .bashrc, and put some code in their.bash_profileto run.bashrc:

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

Starting a Shell

bashRun another Bourne Again shell inside this one

shRun a Bourne shell

cshRun a C shell

zshRun a Z shell

More Reading

The Linux Command Line(free PDF) http://linuxcommand.org/tlcl.php

sedone-linershttp://sed.sourceforge.net/sed1line.txt

Awkhttp://www.grymoire.com/Unix/Awk.html

https://www.gnu.org/software/gawk/manual/gawk.html

Prompt customizationhttps://wiki.archlinux.org/index.php/Bash/Prompt_customization

grephttps://www.gnu.org/savannah-checkouts/gnu/grep/manual/grep.html

results matching ""

    No results matching ""