Here are some essential Linux commands for navigating the file system:
1> ls — > ls command is use to List Files and Directories.
Example :
[swapnil@mylinuxvm root]$ cd
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
in above code we are using cd to go to current user’s HOME directory , we used pwd command to print path and then we are using ls command to see List of Files and Directories.
ls command lists files and directories in the current directory.
1.1> ls -l
- -l : Lists files in long format, showing details like permissions, owner, size, and modification date.
The ls -l command in Linux provides a long listing format for files and directories, displaying detailed information about each entry. Here’s a breakdown of the information it provides:
1. File Permissions:
- First character:
-
: Regular filed
: Directoryl
: Symbolic linkb
: Block special file (e.g., hard drive)c
: Character special file (e.g., serial port)p
: FIFO pipes
: Socket
- Next three sets of three characters:
- Owner permissions:
r
: Read permissionw
: Write permissionx
: Execute permission- Group permissions: Same as owner permissions
- Other permissions: Same as owner permissions
2. Number of Links:
- Indicates how many file names point to the same file or directory.
3. Owner:
- The username of the file or directory’s owner.
4. Group:
- The group name associated with the file or directory.
5. File Size:
- The size of the file in bytes.
6. Modification Date:
- The date and time the file was last modified.
7. File Name:
- The name of the file or directory.
Example :
[swapnil@mylinuxvm ~]$ ls -l
total 0
drwxr-xr-x. 2 swapnil swapnil 6 Oct 26 19:11 Desktop
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Documents
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Downloads
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Music
drwxr-xr-x. 2 swapnil swapnil 6 Oct 27 02:52 Pictures
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Public
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Templates
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Videos
1.2> ls -la : This will list all files and directories in the current directory, including hidden ones, in long format.
-a : Lists all files, including hidden files (starting with a dot).
Example :
[swapnil@mylinuxvm ~]$ ls -la
total 28
drwx------. 14 swapnil swapnil 4096 Oct 12 00:58 .
drwxr-xr-x. 3 root root 21 Oct 12 00:04 ..
-rw-------. 1 swapnil swapnil 665 Oct 27 00:29 .bash_history
-rw-r--r--. 1 swapnil swapnil 18 Feb 15 2024 .bash_logout
-rw-r--r--. 1 swapnil swapnil 141 Feb 15 2024 .bash_profile
-rw-r--r--. 1 swapnil swapnil 492 Feb 15 2024 .bashrc
drwx------. 14 swapnil swapnil 4096 Oct 12 01:17 .cache
drwxr-xr-x. 14 swapnil swapnil 4096 Oct 27 02:52 .config
drwxr-xr-x. 2 swapnil swapnil 6 Oct 26 19:11 Desktop
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Documents
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Downloads
drwx------. 4 swapnil swapnil 32 Oct 12 00:04 .local
drwxr-xr-x. 5 swapnil swapnil 54 Oct 12 00:08 .mozilla
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Music
drwxr-xr-x. 2 swapnil swapnil 6 Oct 27 02:52 Pictures
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Public
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Templates
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Videos
1.3> ls -lah
-h : Displays file sizes in human-readable format (e.g., KB, MB, GB).
[swapnil@mylinuxvm ~]$ ls -lah
total 28K
drwx------. 14 swapnil swapnil 4.0K Oct 12 00:58 .
drwxr-xr-x. 3 root root 21 Oct 12 00:04 ..
-rw-------. 1 swapnil swapnil 665 Oct 27 00:29 .bash_history
-rw-r--r--. 1 swapnil swapnil 18 Feb 15 2024 .bash_logout
-rw-r--r--. 1 swapnil swapnil 141 Feb 15 2024 .bash_profile
-rw-r--r--. 1 swapnil swapnil 492 Feb 15 2024 .bashrc
drwx------. 14 swapnil swapnil 4.0K Oct 12 01:17 .cache
drwxr-xr-x. 14 swapnil swapnil 4.0K Oct 27 02:52 .config
drwxr-xr-x. 2 swapnil swapnil 6 Oct 26 19:11 Desktop
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Documents
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Downloads
drwx------. 4 swapnil swapnil 32 Oct 12 00:04 .local
drwxr-xr-x. 5 swapnil swapnil 54 Oct 12 00:08 .mozilla
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Music
drwxr-xr-x. 2 swapnil swapnil 6 Oct 27 02:52 Pictures
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Public
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Templates
drwxr-xr-x. 2 swapnil swapnil 6 Oct 12 00:04 Videos
[swapnil@mylinuxvm ~]$
2> cd :
If you use cd
without specifying a directory, it will change your current working directory to your home directory. This is equivalent to typing cd ~
.
Here’s a breakdown of what happens:
cd
without a directory: This command tells the shell to change the current working directory.- Default behavior: If no directory is specified, the shell assumes you want to go to your home directory.
- Home directory: This is the directory where you start your shell session and where your personal files are typically stored.
Example:
If your username is user1
and your home directory is /home/user1
, typing cd
alone will take you to /home/user1
.
Additional Notes:
- You can use
cd -
to go back to the previous working directory. - You can use
cd ..
to go up one directory level. - You can use absolute paths (e.g.,
/usr/bin
) or relative paths (e.g.,../Documents
) to navigate to specific directories.
cd with Directory name :
This changes the current directory to the specified directory.
Syntax :
cd directory_name
Example :
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ cd Downloads
[swapnil@mylinuxvm Downloads]$ pwd
/home/swapnil/Downloads
[swapnil@mylinuxvm Downloads]$ cd ../Pictures
[swapnil@mylinuxvm Pictures]$ pwd
/home/swapnil/Pictures
[swapnil@mylinuxvm Pictures]$ cd ..
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$
Explaination:
pwd is used to print current working directory path
ls is used to display list of files and folders in current directory
using cd Downloads we are going inside Downloads directory
Pictures Directory is a one step back available in parallel with Downloads directory so we went one step back using cd .. and then inside Pictures directory using command : cd ../Pictures.
cd .. # Go to the parent directory
cd / # Go to the root directory
3> pwd (Print Working Directory) :
This displays the absolute path of the current working directory.
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$
4> mkdir (Make Directory)
Syntax :
mkdir directory_name
This creates a new directory.
Example :
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ mkdir swapnil_java_notes
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public swapnil_java_notes Templates Videos
[swapnil@mylinuxvm ~]$
In above example mkdir swapnil_java_notes this command created new directory which you can see with name swapnil_java_notes in list displayed.
4> rmdir (Remove Directory)
This removes an empty directory.
Syntax :
rmdir directory_name
Example :
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public swapnil_java_notes Templates Videos
[swapnil@mylinuxvm ~]$ rmdir swapnil_java_notes/
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$
What Happen when we try to remove non-empty directory using rmdir command in Linux ?
Scenario Example :
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ mkdir dir1
[swapnil@mylinuxvm ~]$ ls
Desktop dir1 Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ cd dir1
[swapnil@mylinuxvm dir1]$ pwd
/home/swapnil/dir1
[swapnil@mylinuxvm dir1]$ mkdir dir2
[swapnil@mylinuxvm dir1]$ ls
dir2
[swapnil@mylinuxvm dir1]$ cd ..
[swapnil@mylinuxvm ~]$ pwd
/home/swapnil
[swapnil@mylinuxvm ~]$ ls
Desktop dir1 Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty
[swapnil@mylinuxvm ~]$
in above example we created dir1 and then inside dir1 directory we created dir2 ,when tried to remove dir1 using rmdir we go error as Directory not empty.
How to fix it , we want to delete non-empty directory in linux?
Solution to delete non-empty directory in linux : example : rm -r dir1
[swapnil@mylinuxvm ~]$ ls
Desktop dir1 Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty
[swapnil@mylinuxvm ~]$ ls
Desktop dir1 Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ rm -r dir1
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$
for forceful deletion you can use : rm -rf directory_name
[swapnil@mylinuxvm ~]$ mkdir dir2
[swapnil@mylinuxvm ~]$ ls
Desktop dir2 Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$ rm -rf dir2
[swapnil@mylinuxvm ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[swapnil@mylinuxvm ~]$
This command is a dangerous one if used incorrectly. It stands for:
rm
: Remove a file or directory.-r
: Recursive. This flag tellsrm
to remove directories and their contents recursively.-f
: Force. This flag forces the removal, bypassing prompts for confirmation.
Combined, rm -rf
means to forcefully remove a directory and all its contents, without asking for confirmation.
Caution:
- Irreversible: Once you execute this command, the deletion is permanent.
- Accidental Deletion: Misusing this command can lead to accidental data loss.
- Use with Extreme Care: Only use it when you’re absolutely certain about what you’re doing.
Use rm
with caution, as it permanently deletes files. To remove directories with contents, use the rm -r
command, but be extremely careful.
This removes a file.
rm file_name
rm old_file.txt
By mastering these commands, you can efficiently navigate and manage your Linux file system.