Linux Terminal Cheatsheet

Published by projectsflix on

The world is your oyster, or really the shell is your oyster. What is the shell? The shell is basically a program that takes your commands from the keyboard and sends them to the operating system to perform. If you’ve ever used a GUI, you’ve probably seen programs such as “Terminal” or “Console” these are just programs that launch a shell for you. Throughout this entire course we will be learning about the wonders of the shell.

In this course we will use the shell program bash (Bourne Again shell), almost all Linux distributions will default to the bash shell. There are other shells available such as ksh, zsh, tsch, but we won’t get into any of those.

Let’s jump right in! Depending on the distribution your shell prompt might change, but for the most part it should adhere to the following format: 

date:
date command tells you the current system date

whoami:

Whoami command gives the output of current user using computer

echo

echo with argument prints that argument to output

pwd

pwd stands for ‘print working directory’.it tells you in which folder you are currently working 

cd

cd stands for ‘change directory’

you need to move from one directory to another

for suppose i am in Desktop and want to move to a subfolder ‘python’

then i can do cd python for it

as you can see when i run ‘pwd’ command my path is ./Desktop/python

there are some more options with cd command

  • . (current directory). This is the directory you are currently in.
  • .. (parent directory). Takes you to the directory above your current.
  • ~ (home directory). This directory defaults to your “home directory”. Such as /home/pete.
  • – (previous directory). This will take you to the previous directory you were just at.

these are like symbolic links and we will talk about these later

for now remember these as shortcuts

you can type ‘cd ..’ to go back to previous directory and so on…

ls

ls command is used to list files and folders that are in your current working directory

the output may be different on your console becoz u may have different files and folders

more options with ls

ls -l

-l option gives you long listing of the file 

ls -a 

-a option gives you to see the hidden files in your directory

hidden files?wait what?

files that start with . are hidden from normal view

can u identify hidden files in the above photo?

those are ‘.’  , ‘..’

you saw somewhere those . and ..

yeah u r right in cd command

. is symbolic link to the current folder and .. is symbolic link to your previous parent folder

thats y cd . is current directory and cd .. is changing to previous parent directory

-r -t 

-r option gives output in reverse order

generally ls -l gives u in alphabetical order

but using -r gives u in reverse of alphabetical order

wait what is ls -lr?

its -l and -r options

yeah you can combine options too

-t option gives output from latest modified file to lastly late modified file

combining with -r option

 you get latest modified file 

you can get confused but see the image below

this is the output i got when i executed ls -ltr

so i modified temp file latest

so its the last one and so on …

touch

now lets create our files using command called ‘touch’

touch myfilename 

creates a file with name ‘myfilename’

what if there is already file with that name ‘myfilename’ ?

then it updates it timestamp

it means that we modified (actually we didnot) and it updates timestamp

clear your confusion:

dont worry about symbol  ‘|’ and ‘grep’

we  will discuss later

file

file command gives you the type of the file independent of extension

wait what?

suppose you created a normal text file and named it mytext.jpg 

it will not be a jpg or any image

it will be text file only

cat

cat stands for concatenate

it is used to display the contents in the file and also can display content in multiple files

less

if u have larger content , it would be inconvenient with ‘cat’ command

u can also navigate among the output of less command

after u hit less filename u can use these shortcuts navigate among the output

  • q – Used to quit out of less and go back to your shell.
  • Page up, Page down, Up and Down – Navigate using the arrow keys and page keys.
  • g – Moves to beginning of the text file.
  • G – Moves to the end of the text file.
  • /search – You can search for specific text inside the text document. Prefacing the words you want to search with /
  • h – If you need a little help about how to use less while you’re in less, use help.

when i press G,it takes me to the END OF THE LINE

(remembering san andreas’s last mission? haha lol )

when i press q,i will be quit and my normal prompt appears

history 

wait history? 

this line reminds me of something

history command gives you list of commands you types upto now

clear

clear clears your screen 

but actually you can scroll up and can see previous outputs also

thats just clearing up the mess

cp

now lets try to copy a file from one folder to another

you think like opening explorer and copying right?

no

linux gives u more flexible way of copying

cp filename toitsdestination

it copies file named filename to directory u wish

as you can see i am trying to overwrite the file minsum.py and it asked for it

now you canask that to copy a file we should do this much?

yeah there is a advantage in commands

suppose in a folder i want to copy only python files to another folder

in windows u have to manually select and should copy

or should search for .py and then copy

but in linux we can do this within single line

u can observe *.py

* is a wildcard that means everything(every filename)

we talk about this in regular expressions

as you can see commandline is more powerful

mv 

mv command used to move file from one folder to another

syntax is similar to cp command

mv filename toitsdestination

mv command is also used to rename files

mv newfile newfile2

mkdir

mkdir stands for ‘make directory’

its used to create directories

you can also create subdirectories with -p option

rm

rm command is used to delete files

rmdir command is used to delete folders

but we can delete only when the folder is empty

we can bypass this

using 

-r : recursively

-f : force fully 

rm -rf is dangerous command as it deletes all files without any confirmation

performing that command as root on ‘/’ will delete entire linux filesystem

i am not showing u this command

u do it on your own 😉 

find

find command is used to search for the filename provided and shows the path where that file is located

find /directory -name filename

if you doesnot specify directory find command will search in current directory ‘.’

you can also find folders using -type option

locate

another method to find files using locate command

locate is faster

and uses local database

only disadvantage is u need to update database by

sudo updatedb

when i run locate filename command

help

when you dont know the command’s syntax you can type 

commandname -h 

commandname –help

eg:if i type ls –help it shows me some options i can use 

man

manual pages shows the description and usage of a command

when i type man man

it shows me what are included in manual pages

whatis

whatis command tells the short description about what the command does

eg : whatis cat-> tells you what cat command can do

alias

you can use a alias name for a command

if command is big then u can use this alias command as substitution

if u reboot ur pc

then alias names will be removed

if you want to make it persistent 

add 

alias commadname=’command’

in ~/.bashrc file

so that even after reboot you alias names valids

exit

exit command exits you out of terminal

logout

current user will be logged out

Standard output(stdout)

>  >> operators

as we know that echo will output the text to stdout that is on our screen

we can redirect that output to a file if we wish

we can use > to redirect that output to file

if file ‘temp’ have alredy contents then our text “nikhil” will replace the text which is already present in temp file

so if we want to append to the end of file without replace entire text

we can use ‘>>’ operator

as you can see we have appended successfully

Standard Input(stdin)

we saw different stdouts like screen,file etc

there are different stdins also

common stdin is our keyboard

but we can use contents of a fileas our input also

lets examine what happened

< is stdin operator

cat < temp means we are taking contents in temp file as input to cat command

and then > is redirecting the cat command output to newfile

so our newfile contains the contents of our temp file

Standard Error(stderr)

lets understand stderr by simple example

we try to display a file which doesnot exist

it raises error right?

output is no such file or directory

actually its error 

to store or redirect error messages we need to access using file descriptor

dont worry aboutfile descriptor,its just a number

here is standard descritpors

0 : stdin

1 : stdout

2 : stderr

we can redirect error messages like this

so we redirected error message to error file

if we want to redirect stdout and stderr to file

if you dont want to redirect to any file you can redirect to  a null or garbage

pipe

this is not a command

its a operator

it converts stdout of one command into stdin to another command

confused?

ok lets look at a example

lets breakdown 

ls – command shows u all files and folders in current directory

output of ls command is now input to grep command

grep is another command which used to match the pattern

so in all files and folder names i am looking for ‘wordlist’ using grep

still you confused? its ok let take another example

tee

we can write into two different streams using tee command

now ls will display output on screen and also the output is written into newfile

now you got clarity about how pipe works

Environment Variables

type ‘env’ to see all environment variables

lot of info right?

u can access the variables using $ before their name

for example u can know which user by 

echo $USER

you can also know your shell name by

echo $SHELL

for suppose you have a executable its in a folder somewhere

and you need to access no matter where u r in the path

so inthat case you can set the executable path in PATH variable

so that system checks for executable name in that PATH  and executes it

set PATH=$PATH:/home/myuser/Desktop/

now you can run executables in Desktop directory

cut

cut command is used to extract data from file

with -c option you can get character in that line

as u can see igot 4th character from each line

you can also split sentence with space and you can get specific field

by default cut command takes ‘tab space’ and it will split according to it

its called delimiter

you can change that delimiter using -d option 

suppose i want to get fields separated by space

paste

paste command is used to merge lines into a single line

paste command doesnot save output in file 

if i do cat on new file i have previous content only

the default delimiter for paste also is tab only

you can set delimiter using -d option

head

head command displays top n lines of a file content

eg : head -n 2 displays top 2 lines in my file

tail

tail command is similar to head

but it displays last n lines

eg : tail -n 2 displays last 2 lines in myfile

join

join command is used for joining lines based on a common field

a image is worth 1000 words

if there are no common fields join command wont do anyhting

if common field is not in order you can specify that particular column field

ok lets break down the syntax

-1 is argument 1 that is newfile

-2 is argument 2 that is newfile2

and 1 stands for the field which is common  newfile

2 is field common in newfile2

sort

as the name implies that this comamnd will sort the content in a file alphabetically

with -r option we can reverse sort

tr

tr stands for translate 

this command translates set of characters into another set of characters

credits:Naga Sai Nikhil


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.