Learn Most useful Linux Commands Basic to Advance

Learn most useful commands of Linux OS: how to handle and change permissions of file, directory, create users, enable ports, network setup, IPs, etc
Learn Most useful Linux Commands Basic to Advance - www.pdfcup.com
Most commonly Used Logical Operator
Type Operand Sign Example
Equal to String = $user = 'Tom'
Number -eq $id -eq 1
Not Equal to String != $user != 'John'
Number -ne $id -ne 5000
Greater than Number -gt $count -gt 0
Less than Number -lt $limit -lt 100
Greater than or equal Number -gte $count -gte 0
Less than or equal Number -lte $limit -lte 100

Most commonly Used File Operator
Sign Usage
-d directory Directory exists
-e file File exists
-f file File exists and is a regular file
-r file File is readable
-s file File is not empty
-w file File is writable

Basics Commands:
All opensource packages directory are installed in linux: /etc/opt/sources.list

1. which

This command is used to find the path of the given parameter.


luis@martin:~$ which cat
/usr/bin/cat


2. history

The History command list out the all commands that you have used till now.


luis@martin:~$ export HISTTIMEFORMAT = "%d/%m/%y %T"
luis@martin:~$ history
...
98 20/05/2024 08:33:45 clear
99 20/05/2024 08:34:25 echo "hello"
100 20/05/2024 08:35:05 which python3


3. du

"du" command can be used to find the amount of space taken by a file or directory.
(du -h -max-depth=1 * ) : This combination is used to find the sizes of the file and directory in the current location (in human readable format). The value '1' is representing how much deep it will go under the directory.


luis@martin:~$ du -h -d=1 *
...
5.6M zoom_client_connector.jar
245k mysql_connector.jar
123M Google_Chrome_Setup.exe


4. echo

The 'echo > any_file_name' command can be use to empty a file or delete every content in the file without deleting the actual file.


luis@martin:~$ cat employee_details.txt
1, Tonny Kerlus, Developer
2, Smith Lerry, IT Security Consultant
3, Yarun Keterus, Product Designer
luis@martin:~$ 
luis@martin:~$ echo > employee_details.txt
luis@martin:~$ cat employee_details.txt
luis@martin:~$

5. tar

The "tar" command is use to extract the content from the .tar.gz file.
The "x,z,v,f" parameters are used with tar command:-
The purpose of using -v option is to show all the names of the files being extracted from the 'tar' file.
The -x is a symbol of extracting something from the source file.
The -f symbol is representing that the input is related to some kind of a file.


luis@martin:~$ tar -xvf oracle_setup_35.45.3.tar.gz
...
...
[completed...]
luis@martin:~$
luis@martin:~$ cd  oracle_setup_35.45.3
luis@martin:~/oracle_setup_35.45.3$ ls -ltr
/bin    /temp   /config
/setup  /lib    /barsh.rc
luis@martin:~$
luis@martin:~$ tar -xzvf mysql_3.5.45.tar.gz    # it will also work 

Other option if needed to extract the file in other directory.


luis@martin:~$ tar -xvf oracle_setup_35.45.3.tar.gz  -C /home/martin/Application
...
...
[completed...]
luis@martin:~$
luis@martin:~$ cd /home/martin/Application/oracle_setup_35.45.3
luis@martin:~//home/martin/Application/oracle_setup_35.45.3$ ls -ltr
/bin    /temp   /config
/setup  /lib    /barsh.rc
luis@martin:~$

6. dkpg

dkpg: returns a list of all installed packages on the syatem.


luis@martin:~$ dpkg -l
...
....
[544 more result found, Show all: Y, N: ]: N
luis@martin:~$
luis@martin:~$ # grep helps to return only matching expressions, below command will return only python related packages.
luis@martin:~$ dkpg -l | grep python  
...
....
python.config    python3.sh   python_start.bash


7. cut

Cut command can be used for multi-purpose, here we are using this command to extract some part of data from CSV file.
"-d" is used to declear the delimeter symbol;
"-f" is used to filter the required columns.


luis@martin:~$ cat employee.csv 
emp_id,skill,experience
121,Python,20
125,Java,5
127,Ruby,13
luis@martin:~$
luis@martin:~$ cut -d , -f 1,3 < employee.csv 
emp_id,experience
121,20
125,5
127,13
luis@martin:~$

8. Sort Command:

This command is use to sort the data in Ascending/Descending order. By-default it is sorting the data based on integer number.
The "-t" parameter is used here to declear the delimeter symbol;
The "-d" parameter is used in order to sort column by alphabetically.
The "-r" parameter is used to sort in reverse order.
Here, we are sorting the data based on the first column. So that "-k1,1" is the same as the column numbers in which we want to sort. It can be any integer column, i.e., "-k5,5" or "-k11,11" ).


luis@martin:~$ cat employee.csv 
emp_id,Skill,experience
121,Python,20
129,Java,5
127,Ruby,13
luis@martin:~$ sort -t , -k1,1 < employee.csv
121,Python,20
127,Java,5
129,Ruby,13
emp_id,Skill,experience
luis@martin:~$
luis@martin:~$ sort -t , -k2,2 -d < employee.csv
129,Java,5
121,Python,20
127,Ruby,13
emp_id,Skill,experience
luis@martin:~$
luis@martin:~$ sort -t , -k2,2 -d -r < employee.csv
emp_id,Skill,experience
127,Ruby,13
121,Python,20
129,Java,5
luis@martin:~$

9. Uniq Command:

The "uniq" command is used to fetch the unique value from the source/input date. Additional parameters are using with this commands as:
"-c" : Count the number of unique/duplicate values.
"-d" : Display only duplicate values.
"-u" : Display only unique values.
The unique command will work with the sort command.


luis@martin:~$ cat employee.csv 
emp_id,Skill,experience
121,Python,20
133,Ruby,11
129,Java,5
121,Python,20
367,Rust,5
133,Ruby,11
129,Java,5
luis@martin:~$
luis@martin:~$ sort -t , -k1,1 employee.csv | uniq -u
emp_id,Skill,experience
367,Rust,5
luis@martin:~$
luis@martin:~$ sort -t , -k1,1 employee.csv | uniq -d
121,Python,20
133,Ruby,11
129,Java,5
luis@martin:~$
luis@martin:~$ sort -t , -k1,1 employee.csv | uniq -dc
	2 121,Python,20
	2 133,Ruby,11
	2 129,Java,5
luis@martin:~$

10. WC(Word Count):

The "wc" command returns the total count of lines(-l), words(-w), and characters(-c) from the input file.


luis@martin:~$ cat employee.csv 
121,Python,20
133,Ruby,11
129,Java,5
121,Python,20
luis@martin:~$ wc -l employee.csv
4
luis@martin:~$ wc -w employee.csv
4
luis@martin:~$ wc -c employee.csv
47
luis@martin:~$ wc employee.csv
4 4 47  employee.csv

11. Find:

The "find" command returns all possible matches of the searched file along with the full path.
The "/" symbol representing the directory where we want to search the file. Here, we have given root directory but it can be any exists directory, i.e, /usr/lib/
The "-name" parameter should be use before declearing the file name.
The "*.py" parameter is representing that the file name can be anything but the extension must be "py".
The "|" pipe symbol helps to insert all search-output into the "output.txt" and "tee" parameter will show the real time processing of file search.


luis@martin:~$ find / -name *.py | tee output.txt

luis@martin:~$ find /Downloads -name "*python*"
It will check all the files containing words 'Python' in between under 'Downloads'.

About the author

D Shwari
I'm a professor at National University's Department of Computer Science. My main streams are data science and data analysis. Project management for many computer science-related sectors. Next working project on Al with deep Learning.....

Post a Comment