- 1. Objective
- 2. Important Linux Commands Tutorial
- 2.1. gzip
- 2.2. touch
- 2.3. locate
- 2.4. echo
- 2.5. grep
- 2.6. clear
- 2.7. logout
- 2.8. exit
- 2.9. wc
- 2.10. sort
- 2.11. kill
- 2.12. ps
- 2.13. uptime
- 2.14. sleep
- 2.15. seq
- 2.16. cut
- 2.17. paste
- 2.18. cal
- 2.19. uniq
- 2.20. du
- 2.21. find
- 2.22. diff
- 2.23. join
- 2.24. split
- 2.25. hostname
- 2.26. rev
- 2.27. tar
- 2.28. sudo apt –get install
- 2.29. which
- 2.30. version
- 2.31. top
- 2.32. who
- 2.33. tail
- 2.34. head
This important Linux commands tutorial lists 34 most frequently used Linux commands which are useful for beginners while getting familiar with Linux, Linux commands and terminal.
To install Ubuntu Linux refer Ubuntu installation guide
2. Important Linux Commands Tutorial
Top 34 frequently used Linux commands are given below In this section of Important Linux Commands Tutorial along with their usage-
$ gzip filename
It compresses content of files, gives extension of .gz and needs to be uncompressed before use.
$ touch filename
It creates new empty file. It is another way to create a file. It is also used to change date and time of most recent access and modification.
$ locate filename
It is used to search find files by its name. It searches extremely fast, runs in background to trace the location of files. locate command is fast because it runs in background process in a system. It searches the file and stores them in a database.
$ echo String
It writes arguments to the standard output or display line of text. It is mostly used in batch and scripts. It plays an important role in building shell script.
$ grep “String” filename
It mainly searches the text or searches the file for lines containing a match to the given strings or words. It searches the mention string in given file.
$ grep "String" filename1 filename2
It searches the mentioned String in multiple files mentioned.
It clears the screen which is equivalent to ctrl+L while using keyboard. It looks in the environment for terminal type and then in the terminal database to figure out how to clear the screen.
To exit a login shell or to get out from current session. It can be used by any normal user to end their session.
To exit a shell like log out. It will remind that some jobs are running in background and again writing exit will terminate them and finally it will close the shell.
$ wc [options] filename
Prints the number of newlines, word, bytes in a file. It is used to find out the number of newline count, word count, byte and characters count in files.
$ wc -l filename
Prints the number of lines in a file.
$ wc -w filename
Prints the number of words in a file.
$ wc -c filename
Display the count of bytes of file.
$ sort filename
Simply sorts the file in alphabetical order. It is used for printing lines of input text files and concatenation of all files in sorted order. It takes blank space like field separator .entire input file like sort key. It is important to know that sort command don’t actually sort the files but only print the sorted output.
$ sort -u filename
-u removes duplicate records in a file and only first record is retained.
$ sort -n file
It sorts a file numerically.
It stops a process. It kills or terminates a process without logging out or restarting the system. It sends signal to each process specified by process identifier.
It will display current running process in system. It is also used for view process running on system. It provides an information of current process with detailed information like CPU usage, user id, command name and memory usage.
$ ps -ax
It shows all current running process.
It shows how long system has been running. It will show current time of system, how many users are currently active and system load average
$ sleep number [suffix]
Delay or pause for specified amount of time. It actually causes the system with given amount of time. It suspends the system for mentioned duration then turns on or resume after the duration gets over.
$ sleep (n)
Sleeps for (n) seconds.
$ sleep (nm)
Sleeps for (n) minutes.
$ sleep (nh)
Sleeps for (n) hours.
$ sleep (nd)
Sleeps for (n) days.
$ seq n
Prints number starting from 1 to n. It prints the numbers from first to last with an increment. By default, first and increment are both 1, and each number is printed on its own line. All numbers can be real, not just integers.
$ seq n1 n2
Prints number starting from n1 to n2.
$ cut c(n) filename
Print characters in a line by specifying the position of the characters.
$ cut -c(n), (n) file.txt
To print more than one character at a time by specifying the character positions in a comma separated list.
$ paste filename
Print the content of file. The paste command merges the lines from multiple files.This is a useful tool for merging a single file and also set of files.The paste command sequentially writes the corresponding lines from each file separated by a delimiter on terminal.
$ paste filename1 filename2
Prints the content of file1 and file2 side by side.
Displays a calendar. cal displays a simple calendar. If no arguments are mentioned, the current month is displayed on screen.
$ unique filename
It removes duplicate lines in a file and display unique contents. uniq reports or filters out repeated lines in a mentioned file. It suppress the duplicate lines from a file. It discards all the successive similar lines except one from the input and writes the output.
$ uniq -c filename
It counts occurrence of lines in a file.
$ uniq -dfilename
It prints only duplicate repeated lines.
Display and estimate disk usage in current directory. It is used to show Linux directory size or to know how large subdirectories are, including all the files and subdirectories it contains.
$ du -a
Display and estimate disk usage of all file in current directory.
$ du -h
Display result in human readable format like in kilobytes, megabytes.
List files in current and subdirectories. It is a very useful and easy handy command to search for files from the command line. It can be used to find files based on various search conditions like permissions, user ownership, modification date/time, size etc.
$ find ./directory name
Searches all the files in mentioned directory.
$ diff filename 1 filename2
It displays the difference between two files, best way to find discrepancies b/w files which feels like they are same. It analyzes two files and prints the lines that are different. It gives output as a set of instructions for how to change one file in order to make it identical to the second file.
$ join filename1 filename2
Join two files based on matching or common field of data. Those who know SQL Join it is similar to like that. It combines two files based on the matching content lines found in each file. Using this command is quite easy and if used currently and in the right situation it can save lots of time and effort.
$ split -l filename
Split the file based on the number of lines and then makes a new file of partitioned content where file name will always start with x. It is used to split a file into two or more smaller files; default behaviour of split is to generate output files of a fixed size, default 1000 lines.
$ split -b filename
Specify the number of bytes that each partitioned file should contains.
Prints the hostname, like computer name. It is used to display the system’s Domain Name System or Service or Server name, and to display or set its hostname or Network Information Services domain name.
$ rev String
It reverses the given string. It reverses the order of characters in every line; it copies the specified files to the standard output, reversing the order of each character in every line. If no files are mentioned, the standard input is read.
$ tar -czvf archivename.tar.gz /path/to/directory or file
a.Compress an entire directory or a Single File where :
c: Creates an archive.
-z: Compresses the archive with gzip.
-v: Display progress in the terminal while creating the archive. v is always optional in these commands.
f: Allows you to specify the filename of the archive.
$ tar -xzvf archive.tar.gz
To extract an archive,where:
x – extract files from archive.
2.28. sudo apt –get install
$ sudo apt-get install
apt-get is the command-line tool for working with APT software packages which will install one or more packages desired for installation.
$ sudo apt-get update
It will download the package list from the repositories, updates them to get information on the newest versions of packages and their dependencies.
$ sudo apt-get upgrade
It is used to upgrade all the currently installed software packages on the system.
$ which filename
It shows path of command in your path. It takes one or more arguments where for each of its arguments it prints the full path of the executable. It returns the pathname of the files or link which would be executed in the current environment.
$ filename -version
This command is used to print out version information about OpenSSL (Secure Sockets Layer)or prints OpenSSL version information.
It displays top CPU process. It provides a current progress look at processor activity in real time. It can sort the tasks by CPU usage, memory usage and runtime. It displays a listing of the most CPU-intensive tasks on the system, and provides an interactive interface for manipulating processes.
Display who logged on along with time for current session. It is used to print information about users who are currently active in. It can only see a real user who logged in. It won’t see a user who is online with su command (switch user).
$ tail filename
It displays last 10 lines of mentioned file. It is used to print the last N lines from the file on the terminal. It used with log files to read the last few lines to know about the error messages.
$ tail filename -n 1000
It outputs the last 100 lines of the filename.
$ head filename
Prints first 10 numbers of lines from the mentioned file.
head -n myfile.txt
Display the first n lines of mentioned file. If more than one file is mentioned, head will show the first ten lines of each file individually.