Magazine

Managing Files Using Command-line Tools

Posted on the 18 May 2021 by Satish Kumar @satish_kumar86

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 ofyourcurrent 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 filepathrelative 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/studentdirectory.

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: Thepwd 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: Thelscommand is used to list thedirectorycontents 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:

Option

Description

-a

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

-l

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

-t

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

-r

Lists the contents in reverse order while sorting by filename

-h

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

-S

Sorts the contents by file size

-i

Prints the inode number of each file in listing

-Z

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 yourworkingdirectory.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.Thecdcommand has many options, some of which are described in the following table:

Option

Description

cd -

Changes directory to previous working directory

cdorcd ~

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

Note:

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 currentworkingdirectory 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 optionsusedwith this command are listed in the following table:

Command

Description

cp file1 file2

Copies file1with the namefile2in the current directory

cp file1 file2 /tmp/

Copiesfile1andfile2with the same name to the /tmp/directory

cp file1 /tmp/myfile

Copiesfile1with 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 isusedto 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:

Command

Description

mv file1 file2

Renamesfile1with the namefile2in the current directory

mv file1 file2 /tmp/

Movesfile1andfile2with the same name in the/tmp/directory

mv file1 /tmp/myfile

Movesfile1with a new name,myfile, to the/tmp/directory

mv backup /tmp/

Moves the directory with the namebackupto 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:

Command

Description

mkdir backup

Creates a sample directory backup in the current directory

mkdir /tmp/backup

Creates a sample directory backup under the /tmpdirectory

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 namelinux,windows, andmacin 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

rmisusedto 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:

Command

Description

rm

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

You Might Also Like :

Back to Featured Articles on Logo Paperblog

These articles might interest you :