0% found this document useful (0 votes)
16 views

Linux Cheatsheet

This document provides a summary of common Linux commands and their usage. It discusses basic commands like ls, cd, pwd and touch. It also covers file manipulation commands like cp, mv, rm and mkdir. More advanced commands covered include find, locate, man and alias. The document explains Linux concepts like standard output, standard input, pipes and redirection using >, >> and | operators.

Uploaded by

Sanjay Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Linux Cheatsheet

This document provides a summary of common Linux commands and their usage. It discusses basic commands like ls, cd, pwd and touch. It also covers file manipulation commands like cp, mv, rm and mkdir. More advanced commands covered include find, locate, man and alias. The document explains Linux concepts like standard output, standard input, pipes and redirection using >, >> and | operators.

Uploaded by

Sanjay Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Linux Cheat-sheet

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.

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 def2aults to your “home directory”. Such as
/home/pete.
● - (present directory). This is same as “pwd” command.

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

More options with ls:


“-l” option gives you long listing of the file, it showing files and folders along with
their details.
“-a” option gives you to see the hidden files along with normal files and folders in
your directory.
Hidden files means which are start with ‘.’ and these are hidden from normal view.
Can u identify hidden files in the above photo?
Those are ‘.’ , ‘..’
You saw somewhere those ‘.’ and ‘..’
Yeah you are right in cd command
‘.’ is symbolic link to the current folder and ‘..’ is symbolic link to your previous parent
folder.
That’s why “cd .” is current directory and “cd ..” is changing to previous parent
directory.

“-r” option gives output in reverse order.


Generally “ls –l” gives you in alphabetical order. but using “–r” gives you 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 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.

When I press ‘h’, it shows help content 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” .

There is an advantage in commands


Suppose in a folder I want to copy only python files to another folder, in windows we
have to manually select and should copy them or we should search for “.py” files and
then copy.
But in Linux we can do this within single line

You can observe “ *.py ”


‘ * ’ is a wildcard character which means everything(every filename), here it selects
all the files ends with “.py” .

mv:
“mv” command used to move file from one folder to another.
Syntax is similar to “cp” command
“mv filename to_its_destination” .

“mv” command is also used to rename files.


“mv” filename(existed file name) newfilename(new file name and it shouldn’t be any
other folder name or file name which already existed).

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.

I am not showing you this command, you do it at your own risk.

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

If you reboot your 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 will be valid.

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.

Let’s 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 now our newfile contains the contents of our temp file.

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?

output is” no such file or directory”


Actually it’s error. To store or redirect error messages we need to access using file
descriptor
Here are standard descriptors

0: stdin
1: stdout
2: stderr
We can redirect error messages like this

Here we redirected error message to an “error” file

If we want to redirect stdout and stderr to a file

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.

You might also like