fbpx

Working with Linux Files and Directories

by Wire Tech
Linux Files and Directories

In the 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:

DirectoryDescription
/The root directory, which is the top of the Linux filesystem hierarchy.
/afsDirectory for Andrew File System (AFS).
/bin and /usr/binContain user executable binary files.
/bootContains the bootloader (GRUP) and kernel files necessary to boot up the Linux operating system.
/devContains device files for hardware devices and peripherals attached to the system, and pseudo device files like /dev/null, /dev/zero, and /dev/random.
/etcContains configuration files for the Linux system.
/homeA unified place for users’ files. This directory contains subdirectories; one for  each user. A user’s individual subdirectory serves as the home of this user. It is the default directory that hosts the user when he login to the Linux machine.
/lib , /usr/lib , /lib64 , and /usr/lib64Contain shared libraries.
/mediaA directory to mount external storage media like CD-ROM, DVD, and USB flash/hard-disk.
/mntAnother directory to mount external media, and network file shares.
/optContains optional (third party) software application files, configurations, and executables.
/procA pseudo (not real) filesystem that provides an interface to kernel data structures.
/rootThe home directory of the root user.
/runA temporary filesystem for storing runtime data.
/sbin and /usr/sbinContain system binary executable files.
/srvContains site-specific data.
/sysAnother pseudo filesystem for exporting kernel objects.
/tmpA directory where temporary files are created and used by the operating system, applications, and users.
/usrContains user’s applications, libraries, and executables.
/varA place where continuously changing files are stored. System logs, application logs, crontab logs, and printer spools are all stored here.

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.conf

/etc/sysctl.conf

/home/student/.bash_profile

/dev/zero

/dev/null

/usr/bin/echo

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.conf

dnf/dnf.conf

pki/tls/certs/ca-bundle.trust.crt

sysctl.conf

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.

Linux Files and Directories
The pwd command prints the current working directory.

If you want to list the content of the parent directory, run the command

ls ..
Linux Files and Directories

To change the current directory to /tmp , you have two options: either to use absolute path:

cd /tmp
Linux Files and Directories

or, to use relative path (starting from the current directory /etc) with command:

cd ../tmp
Linux Files and Directories
The command cd is used to change the current directory to the one provided as argument. If no argument provided, it will change to the home directory of the current effective user.  

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 ~
image 53

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.

You may also like

Unlock the Power of Technology with Tech-Wire: The Ultimate Resource for Computing, Cybersecurity, and Mobile Technology Insights

Copyright @2023 All Right Reserved