Skip to content

Latest commit

 

History

History
1182 lines (659 loc) · 18.3 KB

README.md

File metadata and controls

1182 lines (659 loc) · 18.3 KB

linux_commands

linux

Beginner Linux Commands

1. mkdir : Used to make directory

Syntax:

mkdir <directory_name>

It will create a directory with name <directory_name>

mkdir -p <directory_name>/<directory_name2>

It will create a directory with name <directory_name2> inside directory <directory_name>

Example:

mkdir


2. ls : Used to list all files and directories

Syntax:

ls 

It will list all files and directories in current directory

ls -a

It will list all files and directories including hidden files and directories

ls -l

It will list all files and directories and their permissions

Example:

ls


3. cd : Used to change directory

Syntax:

cd <directory_name>

It will change directory to <directory_name>

cd ..
cd ../..

It will change directory to parent directory of current directory

cd ~

It will change directory to home directory

Example:

cd


4. cat : Used to display contents of file

Syntax:

cat <file_name>

It will display contents of file <file_name>

cat <file_name1> <file_name2>

It will display contents of file <file_name1> and <file_name2>

cat <file_name1> <file_name2> > <file_name3>

It will display contents of file <file_name1> and <file_name2> and store it in file <file_name3>

Example:

cat


5. touch : Used to create file

Syntax:

touch <file_name>

It will create a file with name <file_name>

touch <file_name1> <file_name2>

It will create a file with name <file_name1> and <file_name2>

Example:

touch


6. cp : Used to copy file or directory

Syntax:

cp <file_name> <directory_name>

It will copy file <file_name> to directory <directory_name>

cp <file_name1> <file_name2>

It will copy file <file_name1> to file <file_name2>

cp -r <directory_name1> <directory_name2>

It will copy directory <directory_name1> to directory <directory_name2>

Example:

cp


7. mv : Used to move file or directory

Syntax:

mv <file_name> <directory_name>

It will move file <file_name> to directory <directory_name>

mv <file_name1> <file_name2>

It will move file <file_name1> to file <file_name2>

mv <directory_name1> <directory_name2>

It will move directory <directory_name1> to directory <directory_name2>

mv <file_name> <file_name2>

It will rename file <file_name> to <file_name2>

Example:

mv


8. rm : Used to remove file or directory

Syntax:

rm <file_name>

It will remove file <file_name>

rm -r <directory_name>

It will remove directory <directory_name>

rm -rf <directory_name>

It will remove directory <directory_name> forcefully

rm -i <file_name>

It will ask for confirmation before removing file <file_name>

Example:

rm


9. man: manual command

Syntax:

man <command_name>

It will display manual of command <command_name>


10. open: opens the file or directory specified

Syntax:

open <file_name>

It will open file <file_name>

open .

It will open the folder which you are in


11. df : Used to check tha available disk space

Syntax:

df -h

It will display the available disk space in human readable format

Example

df


12. du : Used to check tha size of a file or directory

Syntax:

du -sh <file_name>

It will display the size of file <file_name> in human readable format

du -sh <directory_name>

It will display the size of directory <directory_name> in human readable format

Example

du

Intermediate Linux Commands

13. grep: search for a string in a file

Syntax:

grep <string> <file_name>

It will search for string in file <file_name>

grep -i <string> <file_name>

It will search for string in file <file_name> ignoring case

grep -r <string> <directory_name>

It will search for string in directory <directory_name>

grep -v <string> <file_name>

It will search for string in file <file_name> and display lines which do not contain string

grep -c <string> <file_name>

It will search for string in file <file_name> and display number of lines which contain string

grep -n <string> <file_name>

It will search for string in file <file_name> and display line number of lines which contain string

grep -w <string> <file_name>

It will search for string in file <file_name> and display lines which contain string as a word

grep -A <number> <string> <file_name>

It will search for string in file <file_name> and display lines after the line which contains string

grep -B <number> <string> <file_name>

It will search for string in file <file_name> and display lines before the line which contains string

grep -C <number> <string> <file_name>

It will search for string in file <file_name> and display lines before and after the line which contains string

Example:

grep


14. chmod: change file permissions

To find the permission of a file or directory, use ls -l

access

-rw-r--r-- is the permission of the file.

-rw- is the permission of the owner of the file.

r-- is the permission of the group of the file.

r-- is the permission of the others.

r means read permission.

w means write permission.

x means execute permission.

- means no permission.

r (read): 4 w (write): 2 x (execute): 1 - (no permission): 0

Syntax:

chmod <permission> <file_name>

It will change the permission of file <file_name> to

chmod 777 <file_name>

It will change the permission of file <file_name> to 777(all the access for all the users)

chmod 644 <file_name>

It will change the permission of file <file_name> to 644(read and write access for owner and read access for group and others)

Example

chmod


15. find: find files and directories

Syntax:

find . -name <file_name>

It will search for file <file_name> in current directory

find <directory_name> -name <file_name>

It will search for file <file_name> in directory <directory_name>

find <directory_name> -iname <file_name>

It will search for file <file_name> in directory <directory_name> ignoring case

find <directory_name> -type f

It will search for files in directory <directory_name>

find <directory_name> -type d

It will search for directories in directory <directory_name>

find <directory_name> -empty

It will search for empty files and directories in directory <directory_name>

find <directory_name> -perm 777

It will search for files and directories with permission 777 in directory <directory_name>

find <directory_name> -perm 777 -exec chmod 644 {} \;

It will search for files and directories with permission 777 in directory <directory_name> and change their permission to 644

find <directory_name> -perm 777 -exec rm -rf {} \;

It will search for files and directories with permission 777 in directory <directory_name> and remove them forcefully

find <directory_name> -perm 777 -exec cp -r {} <directory_name2> \;

It will search for files and directories with permission 777 in directory <directory_name> and copy them to directory <directory_name2>

find <directory_name> -perm 777 -exec mv {} <directory_name2> \;

It will search for files and directories with permission 777 in directory <directory_name> and move them to directory <directory_name2>

find <directory_name> -perm 777 -exec grep -i <string> {} \;

It will search for files and directories with permission 777 in directory <directory_name> and search for string in them

find <directory_name> -perm 777 -exec grep -i <string> {} \; -exec rm -rf {} \;

It will search for files and directories with permission 777 in directory <directory_name> and search for string in them and remove them forcefully

-exec is an option that indicates that a command should be executed on each matching file.

{} is a placeholder for the current file name.

\; is a separator indicating the end of the command, like ; in bash.

Example:

find


16. tr : Used to translate characters

Syntax:

tr 'a-z' 'A-Z'

It will translate all the characters from 'a' to 'z' to 'A' to 'Z'

tr -d 'a-z'

It will delete all the characters from 'a' to 'z'

tr -d 'a-z' < file_name

It will delete all the characters from 'a' to 'z' from file <file_name>

tr -d 'a-z' < file_name > file_name2

It will delete all the characters from 'a' to 'z' from file <file_name> and save the output in file <file_name2>

Example

tr


17. head: Used to print top n number of lines for given input

Syntax:

head <file_name>

It will print top 10 lines of file <file_name>

head -n <number> <file_name>

It will print top lines of file <file_name>

head -n <number> <file_name> > <file_name2>

It will print top lines of file <file_name> and save the output in file <file_name2>

Example

head


18. tail: Used to print last n number of lines for given input

Syntax:

tail <file_name>

It will print last 10 lines of file <file_name>

tail -n <number> <file_name>

It will print last lines of file <file_name>

tail -n <number> <file_name> > <file_name2>

It will print last lines of file <file_name> and save the output in file <file_name2>

Example

tail


19. diff is used to display the differences in the files by comparing the files line by line

Syntax:

diff <file_name1> <file_name2>

It will display the differences in the files <file_name1> and <file_name2>

diff -y <file_name1> <file_name2>

It will display the differences in the files <file_name1> and <file_name2> side by side Example

diff


20. history: history command is used to view previously executed commands

Syntax:

history

It will display all the previously executed commands

history | grep <string>


21. alias: used to add alias to commands

Syntax:

alias <alias_name>='<command>'

It will add alias <alias_name> to command

alias <alias_name>='<command>' >> ~/.bashrc

It will add alias <alias_name> to command in file ~/.bashrc

Example

alias


22. wget : Used to download teh files from teh server

Syntax:

wget <url>

It will download the file from the url

wget -O <file_name> <url>

It will download the file from the url and save it as <file_name>

wget -O <file_name> <url> -q

It will download the file from the url and save it as <file_name> and will not display any output

wget -b <url>

It will download the file from the url in background

wget -c <url>

It will download the file from the url and will resume the download if it is interrupted

Advanced Linux Commands

23. awk : A versatile text processing tool for extracting and manipulating data based on patterns and columns.

Syntax:

awk '{print $1}' <file_name>

It will print the first column of the file <file_name>

awk '{print $1,$2}' <file_name>

It will print the first and second column of the file <file_name>

awk '{print $1,$2}' <file_name> > <file_name2>

It will print the first and second column of the file <file_name> and save the output in file <file_name2>

awk '{print $1,$2}' <file_name> | sort

It will print the first and second column of the file <file_name> and sort the output

awk '{print $1,$2}' <file_name> | sort -n

It will print the first and second column of the file <file_name> and sort the output numerically

awk '{print NR,$0}' sample.txt

It will print the line number and the line

awk '{print NF,$0}' sample.txt

It will print the number of fields and the line

awk '{print $1,$2}' <file_name> | sort -n | uniq

It will print the first and second column of the file <file_name> and sort the output numerically and remove the duplicate lines

awk '{print $1,$2}' <file_name> | sort -n | uniq -c

It will print the first and second column of the file <file_name> and sort the output numerically and remove the duplicate lines and print the number of times each line is repeated

Example

awk


24. sed : A stream editor for searching, transforming, and modifying text based on regular expressions.

Syntax:

sed 's/<string1>/<string2>/g' <file_name>

It will replace all the occurences of with in the file <file_name>

sed 's/<string1>/<string2>/g' <file_name> | sort

It will replace all the occurences of with in the file <file_name> and sort the output

sed 's/<string1>/<string2>/g' <file_name> | sort -n

It will replace all the occurences of with in the file <file_name> and sort the output numerically

sed 's/<string1>/<string2>/g' <file_name> | sort -n | uniq

It will replace all the occurences of with in the file <file_name> and sort the output numerically and remove the duplicate lines

sed 's/<sting1>/<string2>' <file_name>

It will replace the first occurence of with in the file <file_name>

sed 's/<sting1>/<string2>/2' <file_name>

It will replace the second occurence of with in the file <file_name>

sed '3 s/<string1>/<string2>/' <file_name>

It will replace the first occurence of with in the third line of the file <file_name>

sed  's/<string1>/<string2>/p' <file_name>

It will replace all the occurences of with in the file <file_name> and print the replaced line twice

sed 'nd' <file_name>

It will delete the nth line

sed '/<string>/d' <file_name>

It will delete all the lines containing

More examples: https://www.geeksforgeeks.org/sed-command-linux-set-2/

Example

sed


25. cut : A tool for extracting specific fields or columns from lines of text based on a delimiter.

Syntax:

cut -d '<delimiter>' -f <field_number> <file_name>

It will print the <field_number> column of the file <file_name> with delimiter

cut -d '<delimiter>' -f <field_number1>,<field_number2> <file_name>

It will print the <field_number1> and <field_number2> column of the file <file_name> with delimiter

cut -c <character_number> <file_name>

It will print the <character_number> character of the file <file_name>

cut -c <character_number1>,<character_number2> <file_name>

It will print the <character_number1> and <character_number2> character of the file <file_name>

cut -c <character_number1>-<character_number2> <file_name>

It will print the characters from <character_number1> to <character_number2> of the file <file_name>

cut -b <byte_number> <file_name>

It will print the <byte_number> byte of the file <file_name>

cut -b <byte_number1>,<byte_number2> <file_name>

It will print the <byte_number1> and <byte_number2> byte of the file <file_name>

cut -c 1- <file_name>

It will print from the first character to the end of the file <file_name>

cut -c -<character_number> <file_name>

It will print from the first character to the <character_number> character of the file <file_name>

Example

cut