Managing files using command-line tools

Commands are names of programs installed on the system. Before proceeding with the basic command operation, let’s have a look at the two types of path traversal in Linux:

  • Absolute path: This method specifies the full path of a file, regardless of your current location. This path always begins with a leading / (root directory) and specifies each subdirectory traversed in order to uniquely represent a single file in the filesystem. This removes any ambiguity whatsoever in the pathname.One directory is separated from another by a forward slash (/) in the pathname.While creating shell scripts, this type of naming convention should be used to refer to a file. Absolute pathnames are long to type in comparison to relative pathnames, which are used frequently when working on the command line to refer to afile or directory.
  • Relative path: This method specifies the file path relative to your current location. It may or may not begin with one or more dot (.) symbol. This path never begins with a /.In this method of traversal, two or more files can have the same relative pathname in the same Linux filesystem,with respect to two different working locations. While working with shell scripts, this type of naming convention should always be avoided, in order to make your script executable from different locations in your Linux filesystem.

For example, if you are working in your home directory, /home/student, and you want to move to the /home/student/backup directory, you can traverse in the following two ways:

  • Absolute pathname traversal: $ cd /home/student/backup
  • Relative pathname traversal: $ cd backup:
files use

The relative path of the backup directory, when a user’s current working directory is /home/student, is simply backup. Here, we specify the directory name only with the cd command, since weare already working inside the /home/student directory.

In a standard Linux filesystem, the pathname of a file, including all characters (even the / directory separator), cannot exceed 4,095 bytes.Most of the time, it is easy to use relative paths for navigation, as it requires less typing. It also takes advantage of the shortcuts provided by meta-characters:

  • .: Dot represents the current directory
  • ..: Double dots represent the parent directory
  •  ~: Tilde represents the user’s home directory

For example, imagine you are working in your home directory, /home/student, and you want to move to the /usr/bin directory; the following two methods can be used:

  • Absolute pathname traversal: # cd /usr/bin
  • Relative pathname traversal: # cd ../../usr/bin

Navigation commands

These commands fall into the category of navigation, as they are mostly used for navigating paths:

  • pwd: The pwd command displays the full pathname of your current working directory.It helps in determining the current syntax to be used with other commands such as cp, mv, rm , mkdir, and so on, using relative pathnames, as shown in the following screenshot:
files use
  • ls: The ls command is used to list the directory contents of the given directory. If no directory name is given, then it lists the contents of the current directory as shown in the following screenshot:
files use

The ls command has got many options that are often used together to produce more structured and human-readable output. The most commonly used options with ls are listed in the following table:




Displays the filenames beginning with a (.), as any filename beginning with (.) is hidden by default


Displays detailed information on contents, also known as the long-listing format


Sorts the listing contents by modification time, with last modified file first


Lists the contents in reverse order while sorting by filename


Prints the sizes of files in human-readable format (for example, 1 K, 50 M, 3 G, and so on)


Sorts the contents by file size


Prints the inode number of each file in listing


Displays the security context (SELinux parameter) for each file

The examples of ls command usage are shown in the following screenshot:

files use
  • cd: Thecdcommand is used to change your working directory.We generally use relative pathnames for brevity while changing directories on the command line. However, while creating scripts, it’s good practice to use absolute pathnames.The cd command has many options, some of which are described in the following table:



cd -

Changes directory to previous working directory

cd or cd ~

Changes directory to user’s home directory

cd ~<username>

Changes directory to the specified <username> user’s home directory

cd . .

Changes directory to up one level to the parent directory

The examples of cd command usage are shown in the following screenshot:

files use


The (. .) represents the parent directory of your current working directory, and (.) represents your current directory in relative pathname format.

File management commands

File management is the process of creating, deleting, copying, and moving files or directories for organizing files logically.When doing file management tasks on the command line, awareness of your current working directory is very important. This will help you to give correct absolute or relative pathnames for the immediate task in hand.

cp is used to copy a file or directory from one location to another. The various useful options used with this command are listed in the following table:



cp file1 file2

Copies file1 with the name file2 in the current directory

cp file1 file2 /tmp/

Copiesfile1 and file2 with the same name to the /tmp/ directory

cp file1 /tmp/myfile

Copiesfile1 with a new name, myfile, to the/tmp/ directory

cp -r backup /tmp/

Copies the backup directory recursively to the/tmp/ directory

The examples of cp command usage are shown in the following screenshot:

files use

mv is used for two purposes. Firstly, it renames a file or directory if the source and destination path of the file are in the same directory. Secondly, it is used to perform cut and paste (move) operations when the source and destination directory are different.

Some of the most frequently used options with the mv command are as follows:



mv file1 file2

Renames file1 with the name file2 in the current directory

mv file1 file2 /tmp/

Moves file1 and file2 with the same name in the /tmp/ directory

mv file1 /tmp/myfile

Moves file1 with a new name, myfile, to the /tmp/ directory

mv backup /tmp/

Moves the directory with the name backup to the /tmp/ directory

The examples of mv command usage are shown in the following screenshot:

files use

mkdir is used to create a directory. This command is also used with different options on the command line, including the following:



mkdir backup

Creates a sample directory backup in the current directory

mkdir /tmp/backup

Creates a sample directory backup under the /tmp directory

mkdir -p backup/linux/centos

Creates directories with full path backup/linux/centos (if parent directories are missing at destination, it will create full path)

mkdir linux windows mac

Creates directories with the name linux, windows, and mac in the current directory

The examples of mkdir command usage are shown in the following screenshot:

files use

rmdir is used to delete empty directories only. If a directory contains subdirectories or files, then we have to use the rm -rf command as shown in the following command:

$ rmdir <empty directoryname>

The examples of rmdir command usage are shown in the following screenshot:

files use

rmis used to delete/remove a file from the filesystem. This command also has multiple options, which are to be used with care as a file once deleted cannot be restored from the trash (the recycle bin of Linux) in command-line mode.

This is a table listing options frequentlyused withthe rm command andtheir descriptions:




Removes a file

rm -f

Forcefully removes a file

rm -i

Interactively removes a file by prompting before each removal (use this if you are uncertain of the filename)

rm -rf

Forcefully remove a directory recursively (use this option very cautiously)

The examples of rm command usage are shown in the following screenshot:

files use

The ln command is used to create links. There are two types of links in Linux, hard links and soft links, which is also known as symbolic link/symlink. The soft links of files and directories can be considered equivalent to the Windows shortcut for files and folders respectively:

  • Hard link creation: $ ln file1 file2
  • Soft link creation: $ ln -s file1 file2

The examples of ln command usage are shown in the following screenshot:

files use

Related Articles

How to Install MongoDB on CentOS 8 Linux

How to Install MongoDB on CentOS 8 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB, which can handle big data. As a MongoDB is a NoSQL database, It stores data in JSON-like documents where fields can...

read more
How to Install OpenCV on CentOS 8 Linux

How to Install OpenCV on CentOS 8 Linux

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial...

read more

Lorem ipsum dolor sit amet consectetur


Submit a Comment

Your email address will not be published. Required fields are marked *

one × 1 =