Example: barber

Linux Bash Shell Cheat Sheet - Learn Code the Hard …

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> --> 't include the '=' in your commands'..' means that more than one file can be affected with only one command ex: rm .. Linux bash Shell Cheat SheetBasic CommandsBasic Terminal ShortcutsBasic file manipulationCTRL L = Clear the terminalcat <fileName> = show content of fileCTRL D = Logout(less, more)SHIFT Page Up/Down = Go up/down the terminalhead = from the topCTRL A = Cursor to start of line-n <#oflines> <fileName>CTRL E = Cursor the end of lineCTRL U = Delete left of the cursor tail = from the bottomCTRL K = Delete right of the cursor-n <#oflines> <fileName>CTRL W = Delete word on the left CTRL Y = Paste (after CTRL U,K or W)mkdir = create new folderTAB = auto completion of file or commandmkdir myStuff.

freeworld.posterous.com Linux Bash Shell Cheat Sheet (works with about every distribution, except for apt-get which is Ubuntu/Debian exclusive) Legend:

Tags:

  Linux, Sheet, Teach, Shell, Bash, Linux bash shell cheat sheet

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Linux Bash Shell Cheat Sheet - Learn Code the Hard …

1 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> --> 't include the '=' in your commands'..' means that more than one file can be affected with only one command ex: rm .. Linux bash Shell Cheat SheetBasic CommandsBasic Terminal ShortcutsBasic file manipulationCTRL L = Clear the terminalcat <fileName> = show content of fileCTRL D = Logout(less, more)SHIFT Page Up/Down = Go up/down the terminalhead = from the topCTRL A = Cursor to start of line-n <#oflines> <fileName>CTRL E = Cursor the end of lineCTRL U = Delete left of the cursor tail = from the bottomCTRL K = Delete right of the cursor-n <#oflines> <fileName>CTRL W = Delete word on the left CTRL Y = Paste (after CTRL U,K or W)mkdir = create new folderTAB = auto completion of file or commandmkdir myStuff.

2 CTRL R = reverse search historymkdir myStuff/pictures/ ..!! = repeat last commandCTRL Z = stops the current command (resume with fg in foreground or bg in background)cp = copy and rename a fileBasic Terminal Navigationcp <folderName>/ = copy to foldercp -a = list all files and folderscp -R stuff otherStuff = copy and rename a folderls <folderName> = list files in foldercp *.txt stuff/ = copy all of *<file type> to folderls -lh = Detailed list, Human readablels -l *.jpg = list jpeg files onlymv Documents/ = move file to a folderls -lh <fileName> = Result for file onlymv <folderName> <folderName2> = move folder in foldermv = rename filecd <folderName> = change directorymv <fileName> stuff/newfileNameif folder name has spaces use mv <folderName>/ .. = move folder up in hierarchy cd / = go to rootcd .. = go up one folder, tip.

3 /../../rm <fileName> .. = delete file (s)rm -i <fileName> .. = ask for confirmation each file du -h: Disk usage of folders, human readablerm -f <fileName> = force deletion of a filedu -ah: files & folders, Human readablerm -r <foldername>/ = delete folderdu -sh: only show disc usage of folderstouch <fileName> = create or update a filepwd = print working directoryln file1 file2 = physical linkman <command> = shows manual (RTFM)ln -s file1 file2 = symbolic linkLinux bash Shell Cheat SheetBasic CommandsResearching FilesExtract, sort and filter dataThe slow method (sometimes very slow):grep <someText> <fileName> = search for text in file-i = Doesn't consider uppercase wordslocate <text> = search the content of all the files-I = exclude binary fileslocate <fileName> = search for a filegrep -r <text> <folderName>/ = search for file namessudo updatedb = update databaseof fileswith occurrence of the textfind = the best file search tool(fast)With regular expressions: find -name <fileName> find -name text = search for files who start with the word textgrep -E ^<text> <fileName> = search start of linesfind -name *text = end with the word textgrep -E <0-4> <fileName> =shows lines containing numbers 0-4 Advanced Search.

4 Grep -E <a-zA-Z> <fileName> = retrieve all lines with alphabetical lettersSearch from file Size (in ~)find ~ -size +10M = search files bigger (M,K,G)sort = sort the content of filessort <fileName> = sort alphabeticallySearch from last accesssort -o <file> <outputFile> = write result to a filefind -name <filetype> -atime -5sort -r <fileName> = sort in reverse('-' = less than, '+' = more than and nothing = exactly)sort -R <fileName> = sort randomlysort -n <fileName> = sort numbersSearch only files or directory sfind -type d --> ex: find /var/log -name "syslog" -type dwc = word countfind -type f= fileswc <fileName> = nbr of line, nbr of words, byte size-l (lines), -w (words), -c (byte size), -mMore info: man find, man locate(number of characters)cut = cut a part of a file-c --> ex: cut -c 2-5 (cut the characters 2 to 5 of each line)-d (delimiter) (-d & -f good for.)

5 Csv files)-f (# of field to cut)more info: man cut, man sort, man grepLinux bash Shell Cheat SheetBasic CommandsTime settings(continued)date = view & modify time (on your computer)crontab = execute a command regularly -e = modify the crontabView:-l = view current crontabdate +%H --> If it's 9 am, then it will show 09-r = delete you crontabdate +%H:%M:%Ss = (hours, minutes, seconds)In crontab the syntax is%Y = years<Minutes> <Hours> <Day of month> <Day of week (0-6,Modify:0 = Sunday)> <COMMAND> MMDD hhmmYYYY Month | Day | Hours | Minutes | Yearex, create the file every day at 15:47:47 15 * * * touch /home/ date 031423421997 = March 14th 1997, 23:42* * * * * --> every minuteat 5:30 in the morning, from the 1st to 15th each month:Execute programs at another time30 5 1-15 * *at midnight on Mondays, Wednesdays and Thursdays:use 'at' to execute programs in the future0 0 * * 1,3,4every two hours:Step 1, write in the terminal: at <timeOfExecution> ENTER0 */2 * * *ex --> at 16:45 or at 13:43 7/23/11 (to be more precise)every 10 minutes Monday to Friday:or after a certain delay:*/10 * * * 1-5at now +5 minutes (hours, days, weeks, months, years)Step 2: <ENTER COMMAND> ENTERE xecute programs in the backgroundrepeat step 2 as many times you needStep 3: CTRL D to close inputAdd a '&' at the end of a commandex --> cp &atq = show a list of jobs waiting to be executednohup: ignores the HUP signal when closing the consoleatrm = delete a job n <x>(process will still run if the terminal is closed)ex (delete job #42) --> atrm 42ex --> nohup cp = pause between commandsjobs = know what is running in the backgroundwith ';' you can chain commands, ex: touch file.

6 Rm fileyou can make a pause between commands (minutes, hours, days)fg = put a background process to foregroundex --> touch file; sleep 10; rm file <-- 10 secondsex: fg (process 1), f%2 (process 2) f%3, .. Linux bash Shell Cheat SheetBasic CommandsProcess ManagementCreate and modify user accountsw = who is logged on and what they are doingsudo adduser bob = root creates new usersudo passwd <AccountName> = change a user's passwordtload = graphic representation of system load averagesudo deluser <AccountName> = delete an account(quit with CTRL C)addgroup friends = create a new user groupps = Static process listdelgroup friends = delete a user group-ef --> ex: ps -ef | less-ejH --> show process hierarchyusermod -g friends <Account> = add user to a group-u --> process's from current userusermod -g bob boby = change account nameusermod -aG friends bob = add groups to a user with-top = Dynamic process listout loosing the ones he's already inWhile in top.

7 Q to close topFile Permissions h to show the help k to kill a processchown = change the owner of a fileex --> chown bob C to top a current terminal processchown user:bob = changes the user to 'user' and the group owning it to 'bob'kill = kill a process-R = recursively affect all the sub foldersYou need the PID # of the processex --> chown -R bob:bob /home/Daniel ps -u <AccountName> | grep <Application>Thenchmod = modify user access/permission simple waykill <PID> ..u = userkill -9 <PID> = violent killg = groupo = otherkillall = kill multiple process'sex --> killall located = directory (if element is a directory)l = link (if element is a file link)extras:r = read (read permissions)sudo halt <-- to close computerw = write (write permissions)sudo reboot <-- to rebootx = eXecute (only useful for scripts andprograms) Linux bash Shell Cheat SheetBasic CommandsFile Permissions (continued)Flow Redirection (continued)'+' means add a rightterminal output:'-' means delete a rightAlex'=' means affect a rightCinemaCodeex --> chmod g+w (add to current group the right to modify )Ubuntumore info: man chmodAnother example --> wc -m << ENDFlow redirectionChain commandsRedirect results of commands.

8 '|' at the end of a command to enter another oneex --> du | sort -nr | less'>' at the end of a command to redirect the result to a fileex --> ps -ejH > and compress data'>>' to redirect the result to the end of a fileArchive and compress data the long way:Redirect errors:Step 1, put all the files you want to compress in'2>' at the end of the command to redirect the result to a filethe same folder: ex --> mv *.txt folder/ex --> cut -d , -f 1 > file 2> '2>&1' to redirect the errors the same way as the standard outputStep 2, Create the tar file:tar -cvf folder/Read progressively from the keyboard-c : creates a .tar archive-v : tells you what is happening (verbose)<Command> << <wordToTerminateInput>-f : assembles the archive into one fileex --> sort << END <-- This can be anything you want> Hello Step , create gzip file (most current):> Alex gzip > Cinema to decompress: gunzip > Game > Code Step , or create a bzip2 file (more powerful but slow):> Ubuntu bzip2 > END to decompress: bunzip2 bash Shell Cheat SheetBasic CommandsArchive and compress data (continued)Installing softwarestep 4, to decompress the.

9 Tar file:When software is available in the repositories: tar -xvf apt-get install <nameOfSoftware>ex--> sudo apt-get install aptitudeArchive and compress data the fast way:If you download it from the Internets in .gz formatgzip: tar -zcvf folder/(or bz2) - Compiling from source decompress: tar -zcvf Documents/Step 1, create a folder to place the file:mkdir /home/username/src <-- then cd to itbzip2: tar -jcvf folder/decompress: tar -jxvf Documents/Step 2, with 'ls' verify that the file is there(if not, mv .. /home/username/src/)Show the content of .tar, .gz or .bz2 without decompressing it:Step 3, decompress the file (if .zip: unzip <file>)gzip:<--gzip -ztf 4, use 'ls', you should see a new directorybzip2:Step 5, cd to the new directorybzip2 -jtf , use ls to verify you have an INSTALL file, tar:then: more INSTALLtar -tf you don't have an INSTALL file:Step , execute.

10 /configure <-- creates a makefiletar extra:Step , run make <-- builds application binariestar -rvf = add a file to the .tarStep : switch to root --> suStep : make install <-- installs the softwareYou can also directly compress a single file and view the fileStep 7, read the readme filewithout decompressing:Step 1, use gzip or bzip2 to compress the file:gzip 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)