Categories
shellinfo tips

Mastering Basic Linux Commands: cd, pwd, ls, df, and du

Linux is a powerful operating system widely used in various environments, from personal desktops to servers and supercomputers. The power of Linux comes from its command-line interface, which allows users to perform complex tasks with just a few keystrokes. In this blog post, we will explore five basic but essential Linux commands: cd, pwd, ls, df, and du.
Navigating Directories with cd

The cd (change directory) command is used to navigate between directories in the Linux file system. It’s one of the most frequently used commands in Linux.

To change to a specific directory, you simply type cd followed by the path of the directory. For example:

cd /home/user/Documents

This command will change the current directory to /home/user/Documents.

To move up one directory level, you can use cd … To go back to the home directory, you can use cd ~ or simply cd without any arguments.
Displaying the Current Directory with pwd

The pwd (print working directory) command is used to display the current directory. It’s a handy command when you’ve navigated deep into the directory structure and want to know where you are.

Simply type pwd and press enter:

pwd

This command will print the full path of the current directory.

Exploring Directory Contents with ls

The ls (list) command is a cornerstone in the Linux command-line interface, used to display the contents of a directory. By default, executing ls with no arguments will list the contents of the current directory.

ls

However, you can also specify a different directory by providing its path as an argument:

ls /home/user/Documents

The true power of ls comes from its various options that allow you to customize the output. For instance, ls -l displays the output in a long listing format, providing detailed information such as file permissions, number of links, owner, group, size, and time of last modification.
ls -l

If you want to view hidden files (those starting with a dot), you can use the -a option:

ls -a

You can even combine options. For example, ls -la will display all files (including hidden ones) in long format.

ls -la

To sort files by modification time, you can use the -t option:

ls -lt

And if you want to view the contents of directories recursively, you can use the -R option:

ls -R

In essence, the ls command is a versatile tool that can be tailored to your specific needs, making directory exploration efficient and user-friendly.
Checking Disk Space with df

The df (disk filesystem) command is used to display the amount of disk space used and available on the filesystems.

df

By default, df displays the disk space in 1K blocks. To make it display the disk space in human-readable form (i.e., in bytes, kilobytes, megabytes, etc.), you can use the -h option:

df -h

Estimating File and Directory Space Usage with du

The du (disk usage) command is used to estimate the space usage of files and directories.

du

By default, du displays the disk usage of the current directory and its subdirectories. To display the disk usage of a specific directory, you can provide the directory path as an argument:

du /home/user/Documents

Like df, du also has a -h option to display the disk usage in human-readable form.
Conclusion

The cd, pwd, ls, df, and du commands are fundamental to navigating and managing the Linux file system. Mastering these commands is the first step towards becoming proficient in Linux. Remember, the key to learning Linux commands is practice. So, open your terminal and start exploring!