Linux Cheatsheet
Linux Cheatsheet
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 operations. 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.
In this course we will use the shell program bash (Bourne Again shell) almost all Linux
distributions will get the bash shell as default. 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 users username.
echo:
echo with argument prints that argument to output.
pwd:
pwd stands for ‘print working directory’ which is present working directory. It tells you
in which folder you are currently working.
cd:
cd stands for ‘change directory’.
If you need to move from one directory to another you have use this command.
For suppose I am at “home/” directory and I want to move to Desktop and subfolders
of it then I can do as in below figure.
ls:
ls command is used to list files and folders that are in your current working directory.
“-t” option gives output from latest modified file to lastly late modified file.
Combining with -r option you get in reverse order.
You may get confused so see the above image
That is the output I got when I executed “ls –tr”
touch:
Now let’s create our files using command called ‘touch’
“touch newfile”.
Creates a file with name ‘newfile’
What if there is already file with that name ‘newfile’?
Then it updates it timestamp, it means that it changes creation time as it newly
created.
Clear your confusion, see the below figure:
Don’t worry about symbol ‘|’ and ‘grep’, we will discuss them later.
file:
“file” command gives you the type of the file independent of extension.
Suppose you created a normal text file and named it “myfile.jpg” .
It won’t be a jpg or any media file, it will be a 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 you have larger content, it would be inconvenient with ‘cat’ command.
But “less” command it is better than “cat” command, you can also navigate among
the output.
After you enter “less filename” you 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 as below.
history:
“history” command gives you list of commands you typed up to now.
clear:
“clear” command clears your screen
But actually you can scroll up and can see previous outputs also.
cp:
Now let’s try to copy a file from one directory to another directory.
You think like opening explorer and copying right?
No, not like that.
Linux gives you more flexible way of copying as follows.
“cp filename to_its_destination” .
mv:
“mv” command used to move file from one folder to another.
Syntax is similar to “cp” command
“mv filename to_its_destination” .
mkdir:
“mkdir” stands for ‘make directory’
It’s used to create directories.
You can also create subdirectories along with directories with a single command by
using “-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 by using
“-r”: recursively
“-f”: force fully
“rm –rf” is dangerous command as it deletes all files without any confirmation.
Performing this command as root on ‘/’(root directory) will delete entire Linux
filesystem.
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 doesn’t specify directory, “find” command will search in current directory ‘.’.
locate:
Another method to find files using “locate” command
“locate” is faster and it uses local database. Only disadvantage is you need to
update database by the following command.
“sudo updatedb”
When I run locate filename command, it is as follows
help:
When you don’t know the command’s syntax you can type
“commandname –h” or
“commandname --help”
Eg: if I type “ls –help” it shows the options that we can use along with the command.
man:
Manual pages shows the description and usage of a command
When I type “man man”.
It shows 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 an alias name for a command
If command is big then u can use this alias command as substitution
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 the screen.
We can redirect that output to a file if we wish, for that we can use > to redirect that
output to file.
If file ‘file1’ already have contents then our text “this is pavankumar guttamalla” will
replace the text which is already present in ‘file1’ file.
If we want to append to the end of file without replace entire text, we can use ‘>>’
operator.
Now you can see we have appended successfully.
Standard Input(stdin)
< operator
We saw different stdouts like screen, file etc.
There are different stdins also, common stdin is our keyboard and files are also used
as stdin. Now we use contents of a file as our input.
Standard Error(stderr)
Let’s understand stderr by simple example.
We try to display a file which does not exist, it raises a error right?
0: stdin
1: stdout
2: stderr
We can redirect error messages like this
If you don’t want to redirect to any file you can redirect to a null or garbage.
pipe ( | ):
This is not a command it’s an operator.
It converts stdout of one command into stdin to another command.
confused? Ok let’s look at this example.
Let’s breakdown
“ls” command shows you 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 or search the pattern
So among all files and folder names I am looking for ‘temp’ using “grep”.
still you get confused? Its ok let’s take another example
tee:
“tee” command reads the standard input and writes it to both the standard output and
one or more files.
Actually “ls” command will shows the list of files and folders as output, now using ‘|’
operator the output of “ls” is moved as input to “tee” command and now the “tee”
command prints the output on the screen as well as writes the output into the file
“list”.
I think now you got clarity about how the pipe( | ) works.