50 Basic Linux Commands for Beginners

Let’s look at the most important and very frequently used commands in Linux. Just be careful about what you are typing in as these are are case sensitive. These commands would help users execute tasks easily and effectively. 

1. ls

In windows, the command dir is used to list the contents in a directory. Similarly in Linux, the ls command is used to list out files and directories. Some versions supports color-coding. So the names in blue represent the directories.

$ ls -l filename

2. pwd

The pwd command is used to find out the path of the current working directory. The command will return an absolute path. Example of an absolute path –  /home/technicalmint.

3. cd

The cd command is the command used to go to a directory. For example, if you are in the abc directory and want to go to the xyz directory, you can simply type “cd xyz”. Remember that this command is case-sensitive and you must type the name of the directory exactly as it appears. The “cd ..” command is used if you want to go back from the current directory to its parent directory. You can run “cd -” command to go back to the previous directory you were in.

4. mv

The mv command is used to move files/directories from one location to another. It can also be used for renaming a file.

# Renaming file a.txt to b.txt
mv a.txt b.txt
# Move file from directory test in your current directory to tmp
mv test/test.txt /tmp/test.txt

# Move multiple files in your current directory to /tmp
mv a.txt b.txt c.txt /tmp 

# Move file from a dir A to dir B by specifying the absolute paths
mv /var/log/test.log /tmp/test.log

5. touch

The touch command is used to create a file. The file can be anything.

For example, you can create a zip file or txt file with same command.

# touch example.txt# ls example.txt

6. cat

The cat (short for concatenate) command is one of the most frequently used commands in Linux. It is used to list the contents of a file on the standard output (sdout). To run this command, type cat followed by the file’s name and its extension.

For instance: cat file.txt.

Here are other ways to use the cat command:

  • cat > filename creates a new file
  • cat filename1 filename2 > filename3 joins two files (1 and 2) and stores the output of them in a new file (3)
  • To convert a file to upper or lower case use, cat filename | tr a-z A-Z > output.txt

7. grep

The grep command searches through many files at a time to find a piece of text you are looking for.

Sintex:

grep PATTERN [FILE]

Example:

# grep error transaction.log

The above command will find all of the words in the files that matched the word ‘error’.

8. cp

The cp command is used to copy files from one location to another. Directories can be copied using the -R option.

# Copy file from your home to directory to another location
cp test.txt /home/technicalmint 

# Copy contents of directories recursively
cp -R test_dir /home/technicalmint

9. rm

The rm (remove) command is used to delete a file or a directory. There is no undo when you delete a file. So use caution when you want to delete something

--> Remove a file in a location
# rm /home/technicalmint/test.txt

--> Remove  a directory that is empty
# rm -r <location_of_dir>

--> Remove a directory with contents in it
# rm -rf <location_of_dir>

10. mkdir

mkdir ( means make directory) is used to create a new directory in a specific location.

--> Create a directory 
# mkdir test_dir 

--> Create intermediate directory (abc) when creating a directory (abc_child) 
# mkdir -p /home/technicalmint/abc/abc_child

11. man & -help

The man command is used to learn more about how a command is used. For example, man ls shows the manual pages of the ls command. If we type a command and add “-help” to it, we get the same result as the manual output.

12. rmdir

If you need to delete a directory, use the rmdir command. However, rmdir only allows you to delete empty directories.

# rmdir /home/technicalmint/empty_dir

13. locate

You can use locate command to locate a file, similar to the search command in Windows.

Note: Using the -i argument along with this command will make it case-insensitive, so you can search for a file even if you don’t remember its exact name.

To search for a file that contains two or more words, use an asterisk (*).

For example, 

# locate -i college*band command will search for any file that contains the word “college” and “band”, whether it is uppercase or lowercase.

14. su / sudo

There are some commands that need elevated rights to run on a Linux system. su command changes the shell to be used as a super user and until you use the exit command you can continue to be the super user.

sudo – if you just need to run something as a super user, you can use the sudo command. This will allow you to run that particular command in elevated rights and once the command is executed you will be back to your normal rights and permissions.

Example – shutdown command safely turns off the system.

--> Shutdown the system after 2 minutes
# sudo shutdown 2

15. passwd

The passwd command is used to reset the user account password. You could reset your password or the password of the other users.

Note: A normal user can only change his/her own password, however root can reset the password for any account.

Sintex:

passwd [username]

Example:

# passwd admin

16. grep

The grep command is very helpful for everyday use. It lets you search through all the text in a given file.

Example:

# grep error all.log

The above command will search for the word error in the all.log file. Lines that contain the searched word will be displayed fully.

17. nano & vi

The nano & vi commands are text editors that are currently installed on the Linux command line. nano is a text editor that recognises most of the languages whereas vi is a little simpler than nano. With this command you can create a new file or you can edit the file using this editor.

Examples:

# vi test

Press i to get into insert mode, make all the changes and then save the file by pressing Esc key on your keyboard to come out of maintenance mode and then :wq and hit enter. If you want to quit and don’t want to make any changes, use :q instead. You can add ! after :wq! or :q! to perform this forcefully in case of any error.

# nano test

Make the changes and press Ctrl+x and Y to make the changes and N to quit the changes.

18. cat

The cat command prints the content of a file on the terminal. There are various other commands you can use to see the content of a file (like less, more, vi, vim, nano, etc.) but this command prints the content to STDOUT (Standard Output).

# cat test

19. df

The df command is used to get a report on the system’s disk space usage i.e., File System details, Mount Points, Used and Available space in a partition, etc. This is shown in percentage and KBs by default. If you want to see the report in MB, run df -m or run df -h to print it in more human readable format.

Example:

[email protected]# df -h
Filesystem Size Used Avail Use% Mounted on
udev 7.6G 0 7.6G 0% /dev
tmpfs 1.6G 1.5M 1.6G 1% /run
/dev/sda1 458G 192G 243G 45% /
tmpfs 7.6G 344M 7.3G 5% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 7.6G 0 7.6G 0% /sys/fs/cgroup
/dev/loop3 145M 145M 0 100% /snap/slack/25
/dev/loop0 55M 55M 0 100% /snap/core18/1880
/dev/loop1 97M 97M 0 100% /snap/core/9665
/dev/loop2 97M 97M 0 100% /snap/core/9804
/dev/loop4 56M 56M 0 100% /snap/core18/1885
/dev/loop5 119M 119M 0 100% /snap/slack/27
tmpfs 1.6G 60K 1.6G 1% /run/user/1001
tmpfs 1.6G 0 1.6G 0% /run/user/0

20. chmod

A file can have r (read), w (write) and x (execute) permissions. The chmod command is used to change those permissions for a file/folder.

Example:

[email protected]# chmod 744 test.sh
[email protected]# ll test.sh 
-rwxr--r-- 1 root root 0 Sep  5 19:24 test.sh

The above command will change the permission of the test.sh file to 744 which is described below in more detail:

Octal Notation Permission Symbolic Representation
0 No Permission
1 Execute Permission Only (1) –x
2 Write Permission Only (2) -w-
3 Write and Execute Permissions (2+1)=3 -wx
4 Read Permission Only (4) r–
5 Read and Execute Permissions (4+1)=5 r-x
6 Read and Write Permissions (4+2)=6 rw-
7 Read, Write and Execute Permissions, Means Full Permissions (4+2+1)=7 rwx

From the table you will see that the rights given will be as

-rwxr- – r- –

  • rwx for user
  • r – – for the group (read only)
  • r – – for others (read only)

Below is the other way of using chmod

# chmod a-w test.sh

This means all users have no write access to the file test.sh.

# chmod u + x test.sh

The owner of test.sh file can execute the file.

21. du

The du is abbreviated as Disk Usage. If you want to check how much space a file or a directory takes, the du command is used. However, the disk usage summary will show disk block numbers instead of the usual size format. If you want to see it in bytes, kilobytes, and megabytes, add the -h argument to the command line.

--> Display disk usage of all files and directories in the current directory 
# du -ah 
-- > Sum of sizes of files and directories in the directory specified 
# du -sh /home/technicalmint

22. uname

The uname command is used to get system information. There are multiple options that can be passed if you want a specific information about the system like kernel-version, processor type, hardware-platform, etc.

--> Prints all the information about the system
# uname -a

23. head

The head command is used to view the first lines of any text file. By default it will show the first ten lines of a file you run it for but you can change this number if you want. Example, if you only want to get the first 20 lines, run below command:

# head -n 20 test_file

24. tail

The tail command is used to see the last lines of any text file. By default, the tail command will display the last ten lines of a text file. Example to get last 5 lines:

# tail -n 5 test_file

25. diff

The diff command is used to compare the contents of two files line by line. After analysing the files, it will give the output of the lines which do not match. We often use this command when we need to make alterations instead of rewriting the entire code.

Example:

# diff file1 file2

26. lsblk

The lsblk command is abbreviated as list block devices and is used to list out all the block devices in a tree-like fashion along with the information about the partitions that are present on the those block devices.

NAME    MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda       8:0    0  55.9G  0 disk
└─sda1    8:2    0  55.9G  0 part  /
sdb       8:16   0 111.8G  0 disk
└─sdb1    8:17   0 111.8G  0 part

27. hostname

The hostname command is used to print or set the hostname of a Linux system. Only the root user or a user with sudo privileges can update the hostname.

--> Print hostname of system 
# hostname
--> Set hostname 
# hostname set name <hostname>

28. file

The file command is used to determine the file type of a given file.

Example:

# file /etc/passwd
/etc/passwd: ASCII text

You can provide more than one file as an argument to the file command if you want.

# file abc.c xyz.out app.java test.png test1.class
abc.c: ASCII C program text, with CRLF line terminators
xyz.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
app.java: ASCII Java program text, with CRLF line terminators
test.png: PNG image data, 1366 x 768, 8-bit/color RGB, non-interlaced
test1.class: compiled Java class data, version 50.0 (Java 1.6)

29. stat

The stat command is used to check the status of a file. It gives detailed information about a file.

Example:

# stat test
File: `test`
Size: 491 Blocks: 8 IO Block: 4096 regular file
Device: 808h/2056d Inode: 149452 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ technicalmint) Gid: ( 1000/ technicalmint)
Access: 2020-09-06 16:07:06.413522009 +0530
Modify: 2020-09-06 16:02:30.204152386 +0530
Change: 2020-09-06 16:17:18.992559654 +0530

30) wc

The wc command is abbreviated as Word Count. It is used to count lines, words and letters of the input given to it.

# wc /etc/passwd
35 59 1724 /etc/passwd

The /etc/passwd file has 35 lines, 59 words, and 1724 letters in it.

Some useful arguments that we can use with wc command:

  • wc -l : Prints the number of lines in a file.
  • wc -w : Prints the number of words in a file.
  • wc -c : Prints the count of bytes in a file.
  • wc -m : Prints the count of characters from a file.
  • wc -L : Prints only the length of the longest line in a file.

31) ln

The ln command is used to create links. Links are nothing but the shortcuts to other files.

Syntex:

# ln TARGET LINK_NAME

Soft links and hard links are the two types of links. By default, a hard link gets created. If you want to create soft link, use -s option.

Examples:

# ln test hard_link
# ln -s test soft_link
# ls -l
total 4
-rw-r--r-- 2 technicalmint technicalmint 491 2020-09-06 14:23 hard_link
lrwxrwxrwx 1 technicalmint technicalmint 10 2020-09-09 14:00 soft_link -> usrlisting
-rw-r--r-- 2 technicalmint technicalmint 491 2020-09-06 14:23 test

32. tar

The tar command is the most used command to archive multiple files into a tarball. A tarball is nothing but a common Linux file format that is similar to the zip format, with compression being optional. This is a complex command with a long list of functions such as creating only tarball, creating a tarball with zip as well, adding new files into an existing archive, listing the content of an archive, extracting the content from an archive, etc. Check out some practical examples to know more about those functions.

33. chown

Every file in our system is owned by a user. The chown command is used to change ownership of a file to the another user.

Example:

# chown technicalmint testfile

The above command will make technicalmint the owner of testfile.

You can also change the group of the file as well by simply adding the group name as well while executing the same command above:

# chown technicalmint:technicalmint testfile

Below is how user and group names are defined in the above example

<username>:<groupname>

34. jobs

The jobs command is used to display all current jobs along with their status. A job is nothing but a process that is started by the shell.

35. echo

The echo command is used to display a text or a string to the standard output or a file.

# echo “Hello World!”

Output – Hello World!

The echo –e option acts as an interpretation of escape characters that are back-slashed.

# echo -e “Hello World! \nIt's time to learn Linux!"

Output –

Hello World!
It's time to learn Linux!

Note: \n the newline character is interpreted by the echo -e command

36. history

The history command is used to get previously used commands or to get information about the commands executed by a user.

# history

37. kill

The kill command is used to kill any process running on your system. It will send a certain signal to process to terminate itself.

There is a total of 64 signals that you can use, but we usually use below two signals:

  • SIGTERM (15): Requests a program to stop running and gives it some time to save all of its progress. If you don’t specify the signal when entering the kill command, this signal will be used.
  • SIGKILL (9): It forces the programs to stop immediately. Unsaved progress will be lost.

Syntex:

kill [signal option] PID.

You can learn more about kill command with some practical examples in detail here – How to kill a process in Linux.

38. ping

The ping command is used to check the connectivity status to a server.

Example:

# ping google.com

The above command will check whether you’re able to connect to Google and also measure the response time or not.

39. wget

The wget command is used to download files from the internet.

Example:

# wget <the download link>

40. alias

The alias is nothing but another name for a command. We can use alias for short names of commands.

Example – Alias for the clear command:

# alias c="clear"

Now if you type “c” and hit enter on your command line, your screen will get clear. Current aliases can be checked with alias command:

# alias

41. w

The w command is used to check which users are logged in to the system and the commands they are executing at that particular time:

# w
10:06:56 up 57 min, 3 users, load average: 0.04, 0.06, 0.09
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root tty1 12:06 28.00s 1.02s 0.67s pager -s
technicalmint tty6 :0 09:19 57:33 1:22 0.20s gnome-session --session=classic-gnome
technicalmint pts/0 :0.0 09:34 0.00s 0.78s 0.00s w

It will also show the uptime, number of users logged in and load average of the system as shown in the example above.

42. last

The last command will show you information about all the users who logged in and logged out on the system. You can learn more about last command in details with practical examples here – Last command in Linux with Practical Examples

43. top

Similar to the Task Manager in Windows system, the top command will display the list of running processes and how much CPU, RAM each process is using. It’s very useful to monitor system resource usage. It’s a very powerful tool which we use in health check, performance management and many other system admin tasks. Below screenshot shows all the fields when we run the top command:

44. fdisk

The fdisk tool is used to get the partition information on the Linux system. It is also used for adding and removing the partitions. The fdisk tool requires rool user privileges.

# fdisk -l

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x396f396f

Device Boot Start End Blocks Id System
/dev/sda1 1 2611 20971520 7 HPFS/NTFS
/dev/sda2 2611 28720 209715200 7 HPFS/NTFS
/dev/sda3 * 28720 38914 81882113 5 Extended
/dev/sda5 28720 33942 41943040 7 HPFS/NTFS
/dev/sda6 33942 34464 4194304 7 HPFS/NTFS
/dev/sda7 34464 35739 10240000 83 Linux
/dev/sda8 35739 36376 5120000 83 Linux
/dev/sda9 36376 36886 4096000 82 Linux swap / Solaris
/dev/sda10 36887 38276 11164672 83 Linux
/dev/sda11 38277 38914 5117952 83 Linux

Below is how we can add a partition:

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Press m for help and then select all the appropriate options to create a partitions.

n – to add new partition

d – in case you want to delete a partition

t – select the type of the partition, need to select the system id here.

w – to write the changes to the disk.

q – to quit without making the changes if you want.

45. zip & unzip

The zip command is used to compress your files into a zip archive and the unzip command is used to extract the zipped files from a zip archive.

Example:

# zip test
# unzip test.gz

You can also use gzip and gunzip in order to compress and extract the files.

# gzip test
# gunzip test.gz

 

Leave a Reply

Your email address will not be published. Required fields are marked *