Working with Linux Files and Directories
Krishnakumar Karancherry
HR Professional | Building Strong Teams for Tomorrow’s Challenges
he previous article, we had an introduction to Files and Directories in Linux. After determining the type of a file, it is time to learn how to work with that file. Another important topic that needs your attention.
So, bring your coffee and … Nice reading!!
As your personal computer’s hard disk is divided into drives C: , D: , E: (and possibly more) that contain files and folders for the Windows operating system, program files, user files, documents, media, and games, etc.?
Filesystems
Linux Files and Directories are contained inside a hierarchical structure called filesystem. So, the concept of filesystems in Linux and UNIX is analogous to hard drives in Windows machines.
A filesystem can be thought of as a logical container that organizes linux files and directories on disk in the form of inverted tree, where the root of the tree is the root filesystem (denoted by forward slash /).
As I told you in the previous article: filesystems are represented to UNIX and Linux users as directories. Therefore, the “grand” parent directory of the whole Linux filesystem structure is the root “ / “ directory.
Directories
Again, directories are files of special type that serve as containers for files and other subdirectories. They provide a way to logically group linux files and directories based on: file types, their use (purpose), the data they store, their owner, or common security permissions on them.
For example, it would be more organized to group together all files related to a specific project. It will be also easier to find and manage to assign a directory for your shell scripts, another for log files, and a third one for software installation package files.
The Linux comes with a list of directories that are created during the operating system installation. These directories are common among almost all Linux distributions.
The following is a collection of the standard pre-created directories and their usages:
In addition to the above automatically-created directories that Linux installer creates during operating system installation, the Linux user can create his own directories.
Pathnames
The Pathname or just Path (for simplicity) of a file or directory is the way or route to follow in order to access the file or directory.
A correctly specified path will lead to a successful access to the file (if the user/process trying to read/manipulate the file has the permission to do).
Conversely, a wrong pathname will mislead the shell, so it will not be able to locate the file or directory you specified.
There are two methods of specifying pathnames: Absolute pathnames, and Relative pathnames.
Absolute Paths
An absolute path to a file or directory is the complete path starting from the top of the filesystem tree (root directory /) down to the target file or directory.
Therefore, absolute paths always start with forward slash ” /?“.
The following are examples of absolute paths:
/etc/systemd/system.con
/etc/sysctl.conf
/home/student/.bash_profile
/dev/zero
/dev/null
/usr/bin/echof
As you see: absolute paths draw the full road from the root of the filesystem tree / to the required object (file or directory), and always start with /.
Relative Paths
The second way is to specify the route to the required file or directory starting from (or relative to) the current directory. For example, if the current directory is /etc , then the following are paths to some files relative to the current directory:
lvm/lvm.con
dnf/dnf.conf
pki/tls/certs/ca-bundle.trust.crt
sysctl.conff
As you could notice: relative paths never start with forward slash, because they are specified assuming the start point is the current directory.
The Special Relative Directories
Two special relative directories are . (single dot) and .. (double dots). The . denotes the current directory. The .. denotes the parent directory.
领英推荐
To illustrate their usage, let’s have some examples. Assume we are still in the /etc directory.
If you want to list the content of the parent directory, run the command?
To change the current directory to /tmp , you have two options: either to use absolute path:
cd /tmp
or, to use relative path (starting from the current directory /etc) with command:
cd ../tmp
Now, /tmp is our current directory. We need to get here a copy from the file /etc/passwd .
To do, we will use the cp command:
cp /etc/passwd .
Here, we specified . as the destination to copy the source file to. It meant we want to copy the file to the current directory.
The command cp is used to copy linux files and directories.
The Tilde ~
As we discussed before when talking about Shell Expansion (refer to Bash Shell – Command Autocompletion & Shell Expansion for more details), the second step that the shell interpreter does when processing a command line is the Tilde ~ expansion. You remember that we said that Tilde is short for the current effective user’s home directory. When it appears in the command line, it is replaced by the full path of the home directory of the user.
For example, to copy the file /etc/passwd to your home directory, run the command:
cp /etc/passwd ~
As we learnt above, if no argument provided with the cd command, it will change to the home directory of the current effective user. Also Tilde ~ is expanded to the user’s home directory.
You may guess now that the following two commands has the same effect:
cd
cd ~
Both commands will change to the home directory of the current user.
There is a lot to say about Linux Files and Directories. Here are some of the common commands for Linux Files and Directories
This article and the one before were just an introduction to this important subject.
So, there is more to learn in the next articles of this Linux series (and all our other series as well). Stay tuned.
Originally published at?https://tech-wire.in?on November 22, 2022.