10 Basic Linux Commands Every Beginner Should know (with Examples)

 

Basic Linux Commands

Are you ready to get hands-on with Linux? This guide walks you through essential basic commands, each with practical examples. Whether you're managing files or checking system info, these examples will help you to become confident on the Linux terminal.

We are covering the following commands in this post.

1.       ls Files and Directory Listing

2.       cd change directory navigate between directories

3.       pwd Command in Linux (Print Working Directory)

4.       mkdir Command in Linux (Make Directory)

5.       rm Command in Linux (Remove Directory)

6.       touch Command in Linux (Create/Update Files)

7.       cp Command in Linux (Copy Files/Directories)

8.       mv Command in Linux (Move/Rename Files & Directories)

9.       cat Command in Linux (File Concatenation & Display)

10.      less Command in Linux (File Viewer)


1.      List files and Directories

    ls    Basic listing command

ls is one of the basic commands of Linux used to show a listing of files and directories. ls has many options that can be used along with the ls command to get output in different ways. We will cover some of the options here with examples.

#ls

•   ls -l   Long format
List the files and directories in a long list style. It includes file and directory permissions, ownership, groups, and modified dates, list one file or directory per line, as shown in the example.
#ls -l

•   ls  -a   Includes hidden files
-a option is used to show all files and directories including hidden files and directories as shown in the example.
#ls -a

•    ls –lh    Human Readable Size
-h flag is used to show the file size in human human-readable format in the output as shown below in the example.
#ls -lh

•    ls -la Long list + Hidden
-a option is used to show hidden files and directories. When it is used with –la, it shows all the hidden files and directories in a long list format.
#ls -la

•    ls -R Recursive List
-R option displays the files and directories in the current directory then files in the individual directories and sub-directories.
#ls -R

•    ls -S  Sort by File Size
-S option displays the files by size from biggest to smallest in descending order.
#ls  -S

•    ls -t   Sort by Modified Time
-t option is used to sort file and directory by date and time in order from the newest to oldest.
#ls -t

•    ls /etc  List Content of Specific Directory
This command lists the content of a specific directory.
#ls /etc

2.     cd (change Directory)
The cd (Change Directory) command is used to navigate between folders in Linux. Here’s how it works with examples:

Basic Syntax
cd [directory_path]

•   Move to a Specific Directory
If you want to move to a specific directory then use this command.
#cd /var/log

•   Change to the /home/seeklinux Directory
Used to change from current directory to /home/seeklinux using absolute path.
#cd /home/seeklinux/

•   go back to Home Directory
This command is used to go back to the home directory from the current directory.
#cd ~

•   Move one Up Directory (parent Folder)
Double dot is used along with cd command to move one directory up from the current directory.
#cd..

•   Move Up two Directories
Moves two directory up from your current directory.
#cd ../  ../

•   Move to the Previous Directory
Brings you back to the previous directory where you were working earlier.
#cd -

•   Move to Root Directory
Takes you to the root (/) directory.
#cd /

•   Navigate Using Relative Path
Moves into the lib directory using relative path.
#cd lib

3.   Pwd Command in Linux (Print Working Directory)
The pwd (Print Working Directory) command displays the full path of your current directory. It’s useful when navigating the Linux file system.

Basic Syntax
pwd [options]

•   Display Current Directory

Show the absolute path of where you are.

#pwd


•   Show Physical Path (Avoid Symlinks)
Show actual physical current working directory by resolving symbolic links (shows the actual path, not the linked one)
#pwd –P

•   What is the Difference between pwd and $pwd
pwd is a command.

$pwd is environment variable to store the current path.

4.  mkdir Command in Linux (Make Directory)
The mkdir command is used to create directories (folders) in Linux. Here’s how to use it with practical examples.

Basic Syntax
mkdir [options] directory_name

•   Creates a Single Directory

Creates a folder named Projects in the current directory.

#mkdir Projects


•   Create Multiple Directories
This command is used to create multiple directories. Creates three folders: Docs, Music and Videos.
#mkdir Docs Music Videos

•   Creates a Directory with a Path
Creates a new folder inside /home/user/Downloads (requires permissions).
#mkdir /home/seeklinux/test-folder

•   Create Nested Directories (Parent + Child)
-p ensures parent directories (Projects/2024) are created if missing.
Final path: ./Projects/2025/May/.

#mkdir -p Projects/2025/May

•   Set Permissions While Creating
-m sets permissions (755 = rwxr-xr-x) while creating directory.
#mkdir -m 755 myprojects

5.   rm Command in Linux (Remove Directory)
The rm command is used to delete empty directories in Linux. Unlike rm -r, it only removes folders with no files/subfolders inside.

Basic Syntax
rmdir [options] directory_name

•   Remove a Single Empty Directory
Deletes the empty folder video (fails if it contains files).
#rmdir videos

•   Remove Multiple Empty Directory
Deletes docs and music (all must be empty).
#rmdir docs music

•   Remove Nested Empty Directory

-p removes parent directories if they become empty.

Deletes May/ → then checks if 2025/ is empty → deletes it → repeats for Projects/.

#rmdir -p Projects/2024/January


•   Verbose Mode (Show Actions)
-v prints a confirmation.
#rmdir -v OldBackup

6.  Touch Command in Linux (Create/Update Files)
Touch command is primarily used to create new empty files and update timestamps (access/modification time) of existing files.

Basic Syntax
touch [options] filename

•   Create Single File
Creates file.txt if it doesn't exist. If it exists, updates its timestamp to the current time.
#touch file.txt

•   Create Multiple Files

Creates three empty files simultaneously.

#touch file1.txt file2.txt file3.txt


•   Create Files with Spaces in Name
Quotes are necessary for filenames containing spaces.
#touch "my document.txt" 'another file.txt'

•   Set Specific Timestamp
-t sets timestamp in format: [[CC]YY]MMDDhhmm[.ss]. Example sets to July 05, 2025, 12:30:00.
#touch -t 202507051230.00 file.txt

•   Update Only Access Time
Updates only the access time (leaves modification time unchanged).
#touch -a file.txt

•   Update Only Modification Time
Updates only the modification time (leaves access time unchanged).
#touch -m file.txt

7. cp Command in Linux (Copy Files/Directories)
The cp command copies files and directories in Linux. It's essential for backups, duplication, and file management.

Basic Syntax
cp [options] <source>   <dest>

•   Copy a File to another Location
This command copies a file to the given destination directory.
#cp file.txt /data

•   Copy with a New Name
This command is used to copy a file with a new name.
#cp file1.txt file2.txt

•   Copy Multiple Files
This command copies multiple files to a directory.
#cp file.txt file1.txt file2.txt data

•   Copy Directory Recursively
This command is used to copy a directory recursively. To copy recursively, the -r option is used with the cp command.
#cp -r data data2

•   Preserve File Attributes
This command is used to preserve the file attributes during copy. It uses -p option for this purpose.

•   Interactive Copy (Prompt Before Overwrite)
We use -i option with the cp command to make the copy operation interactive. This option gives a warning message and waits for the user's confirmation before overwriting the file. The user will press y to continue and n to abort the copy operation.
#cp -i

•   Show Progress While Copying
We use the -v option to see the progress of the copy operation. Normally cp command does not show the progress while the copying operation.
#cp -v file1.txt file2.txt

•   Force Overwrite While Copying
Sometimes the copy operation failed due to a permission denied error because the destination file does not have write permissions. In such a situation to override the permission denied error, a forced copy operation is completed. For this -f option is used with the cp command to copy a file to the destination.
#cp -f -v file1.txt file2.txt

•   Create Hard Links Instead of Copying a File
This option creates a hard link of the file rather than copying a file. This plays an important role when a disk space issue is in place. For this purpose, -l option is used with the cp command.
#cp -i -l file1.txt file6.txt

8. mv Command in Linux (Move/Rename Files & Directories)
The mv command is used to move files/directories to new locations, rename files/directories and overwrite existing files. This command is very handy in day to day operation. Here we will see some practical examples of this command.

Basic Syntax
mv [options] <source> <destination>
mv [options] <file1> <file2> <Directory>

•   Move a File to another Directory
This command is used to move a file to another directory.
#mv file.txt /test

•   Rename a File
A basic operation of the mv command is to rename a file. We will rename file1.txt to file2.txt.#mv file1.txt file2.txt

•   Move Multiple Files
In this example, we move multiple files to a directory.
#mv file1.txt file2.txt file3.txt test

•   Move a Directory
In this example, we move a directory to another directory same as the file is moved to a directory. -v option shows the progress of the operation.
#mv -v test data

•   Move Multiple Directories
The way we moved a directory to another directory, we can move multiple directories to another directory. -v option is used to show the progress.
#mv -v test data docs mydata

•   Interactive Move (Prompt Before Overwrite)
In interactive mode mv command shows a warning message and waits for the user's response to complete the operation. For this purpose, -i option is used with the mv command and y for yes and n for no.
#mv -i -v file1.txt file2.txt

•   Force Overwrite (No Prompt)
Sometimes moving a file to another directory shows a permission denied error and file is not moved to the other directory. In such a case -f option is used with the move command to move the file forcefully.
#mv –f –v file1.txt test

•   Show Progress (Verbose)
This command shows the progress of the file during the move operation. For this purpose -v option is used with mv command.
#mv -v file2.txt file3.txt mydata

•   Overwrite File Only When Source is Newer
This is used to perform an overwrite when the source is newer than the destination. For this purpose -u option is used with the mv command.
#mv -u -v file1.txt file2.txt

9. cat Command in Linux (File Concatenation & Display)
The Cat (concatenate) command is one of the most frequently used commands for displaying file contents, combining multiple files, creating new files and appending to existing files.

Basic Syntax
cat [options] file

•   Display File Contents
The basic function of the cat command is to show file contents. For this purpose, no extra option is used.
#cat mydata/file2.txt

•   Display Content of Multiple Files
cat can show the content of multiple files by providing the name of files separated by a space.
#cat file1.txt file2.txt

•   Create a New File
cat is also used to create a new file that is not already exist on Linux.
#cat basic-commands.txt

•   Append to Existing File
Cat can append the content to the existing file. To append content >> symbol is used.
#cat >> basic-commands.txt

•   Show Line Numbers
If you want to show the line number before each line of file content then use the -n option.
#cat –n basic-commands.txt

•   Combine Files into New File
cat command is also used to copy the content of multiple files into a new file.
#cat file1.txt basic-commands.txt > basic-commands1.txt

10.  less Command in Linux (File Viewer)
Less is a powerful file viewer that lets you read files page by page (unlike cat which dumps the entire file). It's safe for large files and allows searching/navigation.

Basic syntax
less [options] file

•   View a File
less is used to view the content of file.
#less file1.txt

•   View Multiple Files
Open multiple files at the same time without losing the current position of files.
#less file1.txt basic-commands1.txt

•   Search For String
less allows you to search string in an open file. Use / to find the desire string in a file.
#less /LINUX

•   Monitor Growing Files (Like tail –f)

less is also used to monitor a large file slowly and in small chunks as large log file. Use space bar to move file down ward.

#less /var/log/messages


•   Show Line Numbers

less also used to show line numbers. This is especially helpful in large files.

#less –N /var/log/messages


•   Pipe Output
Pipes are used with less command to examine the large files in easy and userstandable way. Especially log files contain lot of data and cannot navigate able. Pipes help for better readability.

#demsg | less


•   Pattern Search
-p option is used to open file containing the first item matching specified pattern. This is case sensitive search.
# less -pdesktop  /var/log/messages


•   less Help
This shows all the related options and switches that can be used with less commands. It is handy in case you forget any option then go to help and see the correct option.
#less –help




























































































 


























Post a Comment

Previous Post Next Post