15 Essential Directory Related Commands in Linux

 


Tamizh Tech Tutorial

Linux Tutorial - Directory Related Commands                 

(15 Examples)

Linux Commands



In this tutorial, you will find the 15 essential directory related commands in the linux / unix.

1. mkdir command

  • This command is used to create a new directory in the linux terminal

  • It takes only one argument which is the name of the directory name

Usages

  • Directory creation

  • Sub directory creation with help of -p option

Syntax 

mkdir directoryname

Where,

directoryname is a user defined name.

Example

Sub-directories creation

  • With the help of option -p, we can create multiple sub-directories under a directory.

Syntax 

mkdir -p existing_directoryname/sd

mkdir -p existing_directoryname/{sd1, sd2, sd3, sd4, …sdn}

Where,

-p indicates parents (parent directory)

existing_directoryname is a name of existing directory name

sd is a name of sub-directory

sd1, sd2,sd3,...sd4 are names of newly created sub directories under the parent directory.

Example

2. rmdir command

  • This command is used to remove (delete) an empty directory

  • It takes only one argument which is the name of the existing empty directory name

Drawbacks

  • It is not possible to remove a non empty directory

  • Directory should not be current directory

Syntax 

rmdir exising_directoryname

Where,

exising_directoryname is the name of an already created directory.

Example - Removing Empty Directory

3. pwd command

  • This command is used to display the path of present working directory (current working directory)

Syntax

pwd

Example

4. ls command

  • This command is used to display the files and folders in current working directory

  • This command is also used to list the files in the specified input directory

Syntax

ls // list files in current directory

ls directory-name // list files in mentioned input directory

Example 1 - Listing files and subfolders in current (present) directory

Example 2 - Listing files and subfolders in specified directory

5. find command (Search for files and directories)

  • This command is also used to display the files and folders in the current working directory or user specified directory using the options d and f.

  • Using option f, this command is used to list only files in current directory or specified directory including sub-directories

  • Using option d, this command is used to list only subdirectories in current directory or specified directory 

Syntax

find directorypath -type d

find directorypath -type f

Where,

directorypath can be the path of the current working directory or user specified directory.

Example 1 - Displaying only all files from user specified directory named test5

Example 2- Displaying only directories from user specified directory named test5

6. cd command

  • cd stands for change directory

  • Quickly switch between the current and previous directories.

  • This command is used to change the path from current directory to path of target directory (location)

Syntax

cd directory-path // move from current path to user specified directory path

cd .. // move from current path to its parent directory

Example - Moving path from current directory to user specified directory named test5

7. tree command

  • This command is used to display the files and folders in current working directory in the tree order (hierarchical order)

  • By default, it will list both files and subfolders in the current directory

  • If you want to print only subdirectories, then use option -d

Syntax

tree // list files and folders in current directory

tree -d // list only subfolders in current directory

tree directory-name // list files and subfolders in user specified directory

Example 1 - Displaying files and folders in current directory

Example 2 - Displaying only subfolders in current directory

Example 3 - Displaying files and subfolders in user specified directory

8. dir command

  • This command is alternative to ls command

  • Like ls command, this command is used to display the files and folders in current working directory

  • This command is also used to list the files in the specified input directory

Syntax

dir // list files & subfolders in current directory

dir directory-name // list files & subfolders in user specified directory

Example 1 - Listing files and subfolders in current (present) directory

Example 2 - Listing files and subfolders in user specified directory named test5

9. cp command

  • cp stands for copy

  • Generally, cp command can be used for both file copying as well as directory copying

  • Using cp command with option -r (recursively), we can copy the contents of one directory into another directory

Syntax

cp src_directorypath target_directorypath // copy files to source to target directory

Example

10. echo command

  • Typically, echo command is used to display the message on terminal

  • With pattern *, it is used to display files and folders in current directory or user specified directory

Syntax

echo * // list files and folders in current directory

echo directory-path/* // list all files in user specified directory using the option /*. Here / indicates the level of directory.

Example 1 - Listing files and folders in current directory

Example 2 - Listing files and folders in user specified directory named test5

11. stat command

  • stat stands for status about file

  • This command is used to display the complete detailed information about a file or directory like file-name, file-size (in bytes), number of links, blocks, file-type, Inode number, etc,.

Syntax

stat file // display detailed the information about a file

Stat directory-name // display the detailed information about a directory.

Example

12. realpath command

  • This command is used to display the absolute path of a file or directory

Syntax

realpath file // display the absolute path of an input file

realpath dir // display the absolute path of an input directory

Example 1 - Displaying the absolute path of given file

Example 2 - Displaying the absolute path of input directory named test5

13. mktemp command

  • This command is used to quickly create a temporary file or directory in the current directory

  • Using option -d, it will create a temporary directory

Syntax

mktemp -d // create a temporary directory

Example - Creating a temporary directory 

14. du command (display the file size)

  • du stands for disk usage

  • This command is used to display the space usage(file size) of file or directory in KB.

  • Here the file or directory can be current or user specified file or directory

Syntax

du file // display the usage about given file

du directory-name // display the usage about given directory

Example - Listing the space usage about file as well as directory named cy1.txt and test5

15. mv command

  • mv stands for move

  • This command is used for moving a file / directory from one location to another location

  • This command is also used to renaming a file / directory

  • This command can be used for both moving as well as renaming a file or directory.

Syntax

mv src_file target_file // rename source file with target file name

mv src_directory target_directory // rename source directory with target directory name

Example - Renaming source directory named dd to target directory named dd1

More Topics

1. How to use linux commands cat, zcat, lolcat, tac with examples and differences 2. For Loop in Bash Script (Shell Scripting)

Tamizh Tech Tutorial










Comments

Popular posts from this blog

Vivo V30 Pro 5G

Xiaomi 14 - Features, Price

Oppo F25 Pro 5G Review - Features, Price