As you transition from Windows or Mac to Linux, you will spend a lot of time working on the Linux terminal. The terminal is a console that accepts commands typed in by a user and executes a task on the system. Running commands on the terminal is an essential skill that any Linux user needs to administer their system efficiently.

Linux provides a vast array of commands, but we will keep it simple in this guide and shed light on 20 basic Linux commands you really ought to know as you get started.

1. pwd

At any given point on the terminal, you are present on a specific directory path. To reveal the path you are working on, run the pwd command. pwd stands for Print Working Directory and is a basic Linux command that displays the full path of the directory you are currently in:

$ pwd
/home/john

The pwd command comes in handy when you are lost on the terminal and would like to know your present working directory.

2. ls

The ls command lists the contents of a directory. In its basic form, it lists all the contents as shown:

$ ls 
Desktop    Downloads         Music      Public   Videos
Documents  examples.desktop  Pictures   Templates

The -l option provides additional information such as file permissions, user and group ownership, file size (in bytes), date and time that the file or directory was last modified, and the file or directory name:

$ ls -l
total 12
drwxr-xr-x 2 john john 4096 Feb 11 09:53 Desktop
drwxr-xr-x 2 john john 4096 Feb 03 11:05 Documents
drwxr-xr-x 2 john john 4096 Feb 03 11:05 Downloads

The -h option prints the file size output in a more user-friendly manner:

$ ls -lh
total 12K
drwxr-xr-x 2 john john 4.0K Feb 11 09:53 Desktop  
drwxr-xr-x 2 john john 4.0K Feb 03 11:05 Documents
drwxr-xr-x 2 john john 4.0K Feb 03 11:05 Downloads

Lastly, you can list files on another directory path by specifying the path to the directory:

$ ls /etc
adduser.conf           init.d                openldap
aliases                initramfs-tools       opt
alternatives           inittab               pam.conf
anacrontab             inputrc               pam.d
apache2                issue                 pciids
apparmor               issue.net             php
apparmor.d             kernel                pki
apport                 ld.so.cache           pm
...

3. cd

The cd command is used to exit the current directory and navigate into other directories.

To navigate to a different directory, specify the full or absolute path from the root (/) directory. This is known as absolute referencing:

$ cd /home/john/Documents

If you are navigating to a subdirectory within your current directory, don’t start with the forward-slash (/). Simply specify the directory name after the cd command. This is known as relative referencing. The relative path is defined from your current working directory and not the root:

$ cd Documents

Without any arguments, the cd command takes you back to your home directory no matter where you are in the terminal:

$ cd
/home/john

4. mkdir

The mkdir command (short for make directory) creates a new directory within the current working directory.

$ mkdir reports

You can also create nested directories using the -p flag:

$ mkdir -p 2020/finance/reports

The command above creates the 2020 directory first, then the finance and reports directories inside 2020.

5. touch

The touch command is used to create a new empty file.

$ touch example.txt

The created file inherits the user and group ownership of the user that created it.

6. rm

rm stands for remove and is used for deleting files and directories.

To delete a file:

$ rm index.html 

To recursively delete a directory and its contents, use the -r flag:

$ rm -r my_folder

7. rmdir

The rmdir command only removes empty directories. Attempting to remove a non-empty directory will result in an error:

$ rmdir my_folder
rmdir: failed to remove ‘my_folder‘: Directory not empty

To delete non-empty directories, use rm -r instead.

8. cp

The cp (copy) command creates a copy of a file or directory.

To copy a file:

$ cp source.txt destination.txt

To recursively copy a directory:

$ cp -r source_dir destination_dir

The difference between copying and moving is that copying retains the original file in its location while moving relocates the file.

9. mv

The mv (move) command either renames or moves a file or directory depending on how it is used.

To rename a file:

$ mv file.txt renamed_file.txt

To move a file to another directory:

$ mv file.txt /path/to/destination  

10. cat

The cat command displays the contents of a file:

$ cat example.txt
This is an 
example file

It can also be used to display multiple files by specifying them one after the other:

$ cat file1.txt file2.txt
This is file 1
This is file 2 

11. less

The less command allows scrolling through the contents of a file instead of printing the full contents to the terminal.

Navigation works as follows:

  • Press spacebar to scroll down
  • Press b key to scroll back up
  • Press q key to quit
$ less long_file.txt

12. head/tail

head and tail print the first and last N number of lines of a file respectively.

For example, to print the first 5 lines:

$ head -n 5 file.txt 

To print the last 10 lines:

$ tail -n 10 long_file.txt

13. gzip/gunzip

gzip reduces file size by compressing files while gunzip restores compressed files back to their original state.

To compress a file:

$ gzip long_file.txt

The original file is then deleted and replaced by a compressed version named long_file.txt.gz.

To decompress:

$ gunzip long_file.txt.gz

This brings the file back to long_file.txt.

14. tar

The tar command archives multiple files and directories into a single .tar file to save disk space.

For example, to archive the contents of a directory named my_data:

$ tar cvf my_data.tar my_data/*

To extract the content of the compressed file:

$ tar xvf my_data.tar

Flags:

c – create an archive
x – extract files from an archive
v – displays verbose output
f – specifies filename for the archive

15. chmod

The chmod command is used to change the permissions (read(r), write(w), execute(x)) of files and directories.

To give a file write permission for its owner:

$ chmod u+w foo.txt

Flag Breakdown:
u – user permissions
g – group permissions
o – others permissions

    • adds permission
    • denies permission

16. chown

The chown command changes the user and group ownership of a given file or directory.

For example, to change the owner of foo.txt to john:

$ chown john foo.txt 

To change both the owner and group to john and developers respectively:

$ chown john:developers foo.txt

17. find

The find command searches for files based on certain criteria:

For example, to find all JPG files:

$ find . -name "*.jpg" 

Some useful options of find include:

-type – search by file type (f for file, d for directory)
-size – search for files above given size
-mtime – search by modification time

18. grep

grep searches files for lines containing a specific search pattern.

Basic usage:

grep ‘search_pattern‘ filename

For example, to print all lines in test.txt containing the word ‘linux‘:

grep ‘linux‘ test.txt

19. ps

The ps command provides information about running processes.

Common options:

  • ps aux – displays details of all running processes
  • ps -ef – prints full format listing of processes

20. kill

The kill command sends kill signal to terminate unwanted processes.

To kill a process:

kill [signal] PID  

Common signals:

SIGTERM (15) – default kill signal
SIGKILL (9) – forces process termination

To list all running processes with their PIDs:

ps aux

Final Thoughts

This sums up some of the most essential Linux commands to help you get started. As you spend more time using Linux, you will learn specialized commands for running servers, network administration, package management and much more. But these basic commands provide a good launch pad for any Linux journey!

Similar Posts