40 Linux Commands for Beginners and intermediate users

Linux is the most popular Operating System when it comes to Servers world, with around 90% of the internet running on Linux.

In this Post, we will list the basic 40 commands that a new Linux user or a Linux System Administrator should be aware of. These commands are used in day to day activities while working on Linux Servers. Note that Linux commands are case sensitive.
Starting with the basic ones.

1. pwd

pwd stands for print working directory, pwd displays the complete path of the current directory from / (root) of the file system.

Basic Syntax of pwd

# pwd 

2. mkdir

mkdir command is used to create one or more directories. It is important to note that a user executing mkdir command must have enough permissions for execution, else it will display “permission denied” error.
You can use following options with mkdir
-p or –parents : creates parent directory as needed.
-v or –verbose : displays message for every directory created.

Basic Syntax of mkdir

# mkdir <options> <directory name>

3. cd

You can use cd command to change or switch to a specific directory.

Basic Syntax of cd

# cd <directory name>
cd ..Used to change current directory to parent directory
cd ~Change directory to user home directory from anywhere
cd –Switch to previous directory where you were working last
cd —Shows last working directory from where we have moved to current path.

4. touch

touch command is used to create empty files. Using touch command, you can also modify the last modification time and other file attributes
You can use below options with touch command
-a : Changes only the access time
-m : Changes only the modification time

Basic Syntax of touch command

# touch <options> <filename>

5. cat

cat command is used to see the contents of a file. cat command can be used to create new files, overwrite existing file and append something to existing files.

Basic Syntax of cat command

# cat [filename]     To see the contents of the file
# cat > [filename]   To create or overwrite a file
# cat >> [filename]  To create or append to a file

6. file

file command displays the file type of a particular file. In Unix, everything is a file whether it is a directory, normal file, device file, socket file etc

Basic syntax of file command

# file <filename>

7. ls

ls command is used to list the files and directories.

Basic syntax of ls command

# ls <options>
Some common options that can be used with ls command:
ls -a : Lists all the files that includes files, directories, hidden files, current directory link (.) and parent directory link (..)
ls -i : prints the inode no of each file
ls -l : lists files and directories in long format.
ls -r : lists the file in reverse or descending order
ls -h : print size of the file in human readable format

8. rmdir

rmdir command is used to remove the directory or complete directory structure.
Note that : rmdir command only removes the empty directories.

Basic Syntax of rmdir

# rmdir <directory name>

9. rm

rm command is used to remove files, directories, symbolic links or any other object from the system.

Basic Syntax of rm command

# rm <options> <file or directory name>
Options:
rm -i : interactive mode, asks for confirmation before removing any file.
rm -f : forcefully deletion, removes the file forcefully.
rm -r : recursively removes directory and its contents
rm -v : displays the verbose message.

10. cp

cp command is used to copy the files or directories from one location to another. Without mentioning any option with cp command, it can copy only files, for copying the directories -r option is used.

Basic Syntax of cp

# cp <options> <source>  <destination>
Options:
-i : interactive mode, asks for confirmation before writing.
-r and -R : recursively copies the directory structure
-f : force copy
-v : displays the verbose message

11. mv

mv command is used to move the files or directories from one location to another.

Syntax of mv

# mv <source> <destination>
Options:
-i : interactive – asks for confirmation before moving.
-v : verbose

12. more

more command is used to navigate or see the contents of a file or output of a command. Using more command, you cannot scroll backward.

Basic Syntax of more command

# more <filename>
# command | more

13. less

less command is also used to see the contents of a file or output of commands page-wise or line-wise. Advantage of less command is that you can scroll back and forth.

Basic Syntax of less command

# less <filename>
# command | less 

14. head

head command be default displays the first 10 lines of a file. Also you can specify the number of lines you want to see with head command.

Basic Syntax of head command

# head <options> <filename>
Options:
-n : Prints the first n number of lines eg. head -n 20 filename.txt
-c : Prints the number of bytes specified with -c option eg. head -c 15 filename.txt

15. tail

tail command by default displays last 10 lines of a file. If you want to see any custom number of lines, use the -n option followed by number of lines and file name. tail is often used to see the logs file.

Basic syntax of tail command

# tail <options> <filename>
Options:
-n : To see any number of lines.
tail -f /var/log/messages : To watch the system logs

16. man

man command is used to see the manual pages for all the command that contains all the options, usage for that command.

Basic Syntax of man command

# man <command>

17. history

history command keeps a list of all the commands that have been run/executed on the system

Basic syntax of history command

# history
Options:
to search for a particular text pattern use grep
history | grep <text_pattern>
history -c : Clears the history

18. cal

cal command displays the calendar, you can choose following options with cal command.

Basic Syntax of cal

cal
Options:
cal 12 2018 : Displays selected month and year on the console
cal 21 12 2018 : Display the day, month and year calendar on console, also highlights the day.
cal -1 : Displays the calendar of current month
cal -m : Displays monday as the first day of the week

19. date

date command is used to view or set date and time of the Linux system.

Basic Syntax of date

# date

20. su

su command is used to temporarily change to a different user that means to switch from one account to another. User will be prompted to enter password while running su command.

Basic syntax of su command

# su - <username>

21. echo

echo command is used to display a line of text or strings on the screen or append the file with text. echo is generally used in shell scripts for displaying any message.

Basic Syntax of echo

# echo <options> <strings>
Options
-e : enable back slash escape character for escape characters like \\, \a,\n, \t, \v
echo *.tar : will print all the files ending with .tar extension
echo “Hi, Welcome to linuxforgeek” >welcomepage [can be used as redirect operator]

22. uptime

uptime command is quite common and it is used to check the system’s uptime that means the date and time since the system is up. It also gives additional information like load average of cpu since 1, 5 and 15 minutes and number of users logged into the system.

Syntax

# uptime

23. clear

clear command is used to clear the screen

Syntax

# clear

24. hostname

hostname command displays the hostname or machine name configured for the system.

Syntax

# hostname

25. id

id command is used to print the username, uid, gid and groups of the user. For checking a specific user information use id <user>

Syntax

# id

26. who

who command displays following information:
Users who are connected
terminal to which they are connected
date and time since they are logged in
remote hostname of the user

Syntax of who command

# who

27. w

w command display more information than who command and you ay want to use w instead, it display the logged into the Linux and what are they doing that means current running process.

Syntax of w

# w

28. whoami

if you want to know which user you are currently logged in. The output of whoami shows the current user.

Syntax

# whomai

29. whatis

whatis command is used to know one line or brief description of Linux commands.
example :$ whatis top will show short one liner description about top

Syntax

# whatis <keyword>

30. which

which command gives the complete absolute path of a particular command.

Syntax

# which <command>

31. wc

wc command can be used to display the bytes, lines, and characters of a file.

Syntax

# wc <options> <filename>
Options
-l : print the lines count.
-w : prints the word count.
-c : prints the bytes count.
-m : prints the character count.

32. ln

ln command is used to create softlink or hardlink. links are references or alias to the same file with different name.

Syntax

# ln <options> <source> <destination>

33. alias

alias command is used to display all the configured aliases on the system, also it can be used to set aliases.

Syntax

# alias <arguments>
eg. alias cl=’clear’
If you want to make the alias permanent, you need to add the same in your ~/.bashrc file.

34. unalias

If you want to remove the alias from the system, use unalias command.

Syntax

# unalias <alias>

35. uname

uname command displays the system information. It can be used with various options.

Syntax

# uname <option>
Options:
-a : all information
-s : Kernel name
-m : machine type
-n : nodename
-p : prints the processor type
-o : operating system
-r : kernel version

36. uniq

uniq command in linux is used to remove the duplicate lines or filters repeated lines in a file

Syntax

 # uniq <options> <filename>
Options:
-c : counts the number of occurrences or how many times a line is repeated
-d : only prints duplicate lines
-i : ignore case sensitivity

37. sort

sort command is used to sort the contents of a file in ascending or descending order. In case no option is specified, sorting is done in ascending order.

Syntax

# sort <options> <filename>
Options:
-r : to sort in descending order.
-n : to sort a file numerically

38. find

find command is one of the powerful tool in Linux. It can be used to recursively look for files matching selection criteria.

Syntax

# find <path> <selection criteria>  <action>
<path> : You can specify the absolute or relative path where you want to find the files. example /var , /etc/ or /
<selection criteria> : Following are the selection criteria that can be used with find command.
-atime <n>: file accessed last <n> days ago
-ctime <n>: file who attributes are changed last <n> days ago
-mtime <n>: modified <n> days ago
-type <x> : to filter type of file. It can be following
f : ordinary files
d : directory
l : symbolic links
s : socket files
p : named pipes
c : character device files
b : block device files
-links (n) : find the files having n number of hard links
-user (uid) : find the files that are owned by a specific uid/username
-group (gid) : find the files that are owned by a specific gid/groupname
-name (filename) : find the files with its name.
-perm : to search using specific permissions
<action> : Following action can be used with find
ls : for listing files
-print :to print output on the screen
-exec <cmd> {}\; To execute command <cmd> on the output.

39. grep

grep command is used to search for a specified string pattern in the files or output of commands.

Syntax

# grep <options> <string pattern> <filename/command>

40. ping

You can use ping command to check connectivity with a particular server or IP address.
example :ping google.com will check connection to google.com

Syntax

# ping <IP or hostname>
We hope you liked this tutorial, keep visiting linuxforgeek.com for more interesting tutorial, Please feel free to comment below.