Every file in the Linux file system is structured in a single inverted tree directory called file system hierarchy. These files are rooted at /. The / directory is the root directory which holds other directories and subdirectories; you can choose to add more directories and subdirectories to it by mounting them or remove the directories by unmounting them.

Let me narrow this down to basic understanding. In windows machine which almost everyone is conversant with; (windows 10 to be precise). When you click on “This PC,” you navigate to where you have “Local disk C.” Local disk C is the root directory of your windows machine, and it holds so many directories and subdirectories that contain different data for different functions such as the “program files” which hold every installed application by default. This is also the same with Linux machines; every directory is located on the root directory. For example, “etc” is a subdirectory of the / directory and we call that directory /etc. If the /etc directory holds another file named chaly we will refer that file as /etc/chaly.

Subdirectories in Linux have unique purposes in organizing files based on their functions in the system. This makes it easy in locating files. For example, the /etc directory holds configuration files specific to the system.

This article will ground you on the basic commands on how to manipulate files and directories

                  IMPORTANT LINUX DEFAULT DIRECTORIES

As we said, Linux has special directories which hold special files that perform various functions in Linux. These directories are not expected to be modified or deleted for any reason as doing so might obstruct the functionality of your system. So it is expected to always log in as a regular user if you don’t know your way about on how to handle Linux files to avoid deleting those files. The superuser which is the root user has the absolute right to any file and directories on your system, so it is necessary not to allow others to bridge the security of your system by gaining the superuser access.

To access your Linux system files, navigate to Places on the GUI shell and from the drop-down menu click on Computer. Below are some vital Linux directories specific to the system

/etc: it holds system configuration specific to the system

/var: it holds variable data specific to the system. Files that dynamically change such as log files

/run: It is meant for system daemons and holds temporary runtime files like PID files

/home: This is the home directory of the local users, where their information and configuration files are stored

/root: The home directory of the superuser

/tmp: This is a space for temporary files. It doesn’t hold files for long and files that have not been accessed within ten days a deleted to create space for new ones

/boot: This holds files needed to boot the system. Files in this directory are not expected to be deleted because it will prevent your system from booting up

/dev: It holds device files used by the system to access the hardware

                                   NAVIGATING FILES THROUGH ITS PATH

The path of a file or directory is the unique file system location. Locating a file, you have to traverse through one or more directories, and subdirectories, delimited by forward-slash (/) until the destination is reached.

A file in a Linux system can either be located through its absolute path or relative path. Finding a file through its absolute path begins from the outermost directory (/), which is the root directory and specifying each subdirectory separated by “/ “until the destination is reached.

For example, let’s say I want to go to my neighbor’s house down across the street from my room, I have to list every single path I traverse to get to my neighbor’s house; starting from my room. My room, in this case, is the root directory (/).

Traversing through absolute path can be tasking when typing, so files can also be located through its relative path. The relative path doesn’t have to traverse through the entire subdirectories to get to its destination; it only traverses through only the path necessary to reach the file from the working directory. A relative path is denoted by not having the forward-slash as its first character.

The pwd and the cd command are basic commands necessary to view and navigate through paths, respectively. The pwd command displays the full pathname of the current location, which helps to determine the appropriate syntax to get to a destination file. The cd command helps to switch between directories. You can specify the absolute path or the relative path while working with the cd command. You can as well move up one directory level by using “cd.. command.” Without needing to know the exact parent name

                                 COMMAND LINE FILE MANAGEMENT

Managing files from the command line involve creating, moving, deleting, and copying files and directories. While performing these operations, you should be aware of your current working directory to enable you to determine how to specify your paths explicitly.

                                              HOW TO CREATE A FILE

The touch command is used to create one or more empty files; it can also be used to update the timestamp of a file without modifying it. Touch has other options attached to it to make it act differently while creating files.

                                        HOW TO CREATE A DIRECTORY

The mkdir command is used to create one or more directories and subdirectories, which is similar to folders in Windows operating systems. The command can create multiple directories and set permissions on them. As a regular user with no sudo access, you are restricted to creating accounts in the parent directory.

You noticed that we successfully created two directories using the mkdir command
but couldn’t create a directory while trying to create subdirectories in a directory. This can only be done using the –p parent option

                          HOW TO COPY FILES AND DIRECTORIES

To copy files, we use the cp command. The cp command allows copying one or more files to become an independent file and also copy directory to another directory. It allows copying an existing file to a new file within the current directory or in another directory or copying multiple files to another directory. When a file is copied to a new directory with the same filename as the one being copied, the cp command overrides the existing file.

Note that, you cannot copy a directory to a file, files can only be copied to a directory. Content of a file can be copied to another file, the content of a directory can be copied to another directory, and the content of a file can’t be copied to a directory

The cp command operates in three modes depending on the type and number of
arguments passed on it.

Specifying A Source File With The Same Filename As The Destination File: When using the cp command to copy a file with the same filename as the destination file, it overwrites the destination file with the new file without any warning. If no file exists, it creates a new file and copies the content of the source file to it.

You noticed that “myfile2” in roots home directory has the same name with “myfile2” in Downloads and copying the same filename forced the command to overwrite the file content

Specifying One or More Arguments: If the command has an argument specifying the files to be copied and another argument specifying the destination directory, the cp command will copy all the files specified as an argument to the destination directory. If the same filename exists in the destination directory, the cp command will overwrite the files in the destination directory with the new files.

You can specify more than one files to be copied to a destination. If the same filename name exists in the destination folder, it will be overwritten without warning

Specifying Two Directory Names As The Argument: If a source and destination directory are specified as the argument, the cp command copies the content of the source directory to the destination directory. This can be done by appending the –R option after the command. If the destination directory doesn’t exist, the cp command creates the directory and copies the content of the file to the directory, but if it exists, the cp command copies the files in the source directory recursively to the destination directory.

Note: Recursively means going deep into the directory to copy its content

The first cp command wasn’t successfully copied because of the absence of –R recursive option

                                 HOW TO MOVE FILES AND DIRECTORIES

The mv command renames files in the same directory or moves files to a new directory, but they do not affect the content of the directory. It supports moving single and multiples files or directories from one place to another.

In the first mv command, you notice the result is a rename. Moving an existing file to a file that doesn’t exist force the file to rename to the new file but in the second mv command, the result is a move

                                 HOW TO REMOVE FILES AND DIRECTORIES

The rm command removes or deletes files and directories. Using the rm command alone deletes files alone but not directories. To delete directories or the subdirectories, the “–r recursive option” is required. They are no command to restore deleted files. Appending “–f or –i” after the command will forcefully delete files and prompt user for each deletion respectively. The rmdir command deletes empty directories.

You noticed that rm command was able to remove files successfully but throw an error while trying to use the same command to remove directories. To successfully remove directories, we have to include the “–r recursive option.”

                                                          CONCLUSION

They are several options available while working on file management, and all can’t be discussed here. It is best you make use of the man command to see how different options work while managing files from the command line

REFERENCES

Mv man page

cp man page

touch man page

mkdir man page

Red Hat Enterprise Linux 7 System Administration II Course

Author

Comments