Linux - File Management Commands
Linux - File Management Commands
437.7K
This article is Part 2 of the LFCA series, here in this part, we will explain about
Linux file system and cover the basic file management commands, that are
required for the LFCA certification exam.
As you get started out in Linux, you’ll spend a great deal of time interacting with
files and directories. Directories are also known as folders, and they are
organized in a hierarchical structure.
In the Linux operating system, each entity is regarded as a file. In fact, there’s a
popular statement in Linux circles that goes: ‘Everything is a file in Linux’. This is
just an oversimplification and in the real sense, most files in Linux are special
files that include symbolic links, block files, and so on.
1. Regular Files
These are the most common file types. Regular files contain human-readable
text, program instructions, and ASCII characters.
These are files that represent physical devices such as mounted volumes,
printers, CD drives, and any I/O ) input and output device.
3. Directories
A directory is a special file type that stores both regular and special files in a
hierarchical order starting from the root ( / ) directory. A directory is the
equivalent of a folder in the Windows operating system. Directories are created
using the mkdir command, short for making the directory, as we shall see later on
in this tutorial.
The Linux hierarchy structure starts from the root directory and branches out to
other directories as shown:
• The /root directory is the home directory for the root user.
• The /dev directory contains device files such as /dev/sda.
• Static boot files are located in the /boot directory.
• Applications and user utilities are found in the /usr directory.
• The /var directory contains log files of various system applications.
• All system configuration files are stored in the /etc directory.
• The /home directory is where user folders are located. These include
Desktop, Documents, Downloads, Music, Public, and Videos.
• For add-on application packages, check them out in
the /opt directory.
• The /media directory stores files for removable devices such as USB
drives.
• The /mnt directory contains subdirectories that act as temporary
mount points for mounting devices such as CD-ROMs.
• The /proc directory is a virtual filesystem that holds information on
currently running processes. It’s a strange filesystem that is created
upon a system boot and destroyed upon shutdown.
• The /bin directory contains user command binary files.
• The /lib directory stores shared library images and kernel modules.
Linux File Management Commands
You will spend a great deal of time interacting with the terminal where you will be
running commands. Executing commands is the most preferred way of
interacting with a Linux system as it gives you total control over the system
compared to using the graphical display elements.
For this lesson, and the coming lessons, we will be running commands on the
terminal. We are using Ubuntu OS and to launch the terminal, use the keyboard
shortcut CTRL + ALT + T .
Let’s now delve into the basic file management commands that will help you
create and manage your files on your system.
1. pwd Command
pwd, short for the print working directory, is a command that prints out the current
working directory in a hierarchical order, beginning with the topmost root
directory ( / ) .
To check your current working directory, simply invoke the pwd command as
shown.
$ pwd
The output shows that we are in our home directory, the absolute or full path
being /home/tecmint.
2. cd Command
For instance, to navigate to the /var/log file path, run the command:
$ cd /var/log
Navigate Directories in Linux
To go a directory up append two dots or periods in the end.
$ cd ..
To go back to the home directory run the cd command without any arguments.
$ cd
cd Command Examples
NOTE: To navigate into a subdirectory or a directory within your current directory,
don’t use a forward slash ( / ) simply type in the name of the directory.
For example, to navigate into the Downloads directory, run:
$ cd Downloads
3. ls Command
$ ls
From the output, we can see that we have two text files and eight folders which
are usually created by default after installing and logging in to the system.
$ ls -lh
$ ls -la
This displays hidden files which start with a period sign (.) as shown.
.ssh
.config
.local
List Hidden Files in Linux
4. touch Command
The touch command is used for creating simple files on a Linux system. To
create a file, use the syntax:
$ touch filename
$ touch file1.txt
5. cat Command
$ cat filename
6. mv Command
$ mv filename /path/to/destination/
For example, to move a file from the current directory to the Public/docs
directory, run the command:
$ mv file1.txt Public/docs
$ mv /path/to/file .
We are now going to do the reverse. We will copy the file from the Public/docs
path to the current directory as shown.
$ mv Public/docs/file1.txt .
$ mv filename1 filename2
$ mv file1.txt file2.txt
$ mv file1.txt Public/docs/file2.txt
Move and Rename Files in Linux
7. cp Command
The cp command, short for copy, copies a file from one file location to another.
Unlike the move command, the cp command retains the original file in its current
location and makes a duplicate copy in a different directory.
$ cp /file/path /destination/path
For example, to copy the file file1.txt from the current directory to
the Public/docs/ directory, issue the command:
$ cp file1.txt Public/docs/
$ cp -R tutorials Public/docs/
8. mkdir Command
You might have wondered how we created the tutorials directory. Well, it’s pretty
simple. To create a new directory use the mkdir ( make directory) command as
follows:
$ mkdir directory_name
$ mkdir projects
Create Directory in Linux
To create a directory within another directory use the -p flag. The command
below creates the fundamentals directory inside the linux directory within the
parent directory which is the projects directory.
$ mkdir -p projects/linux/fundamentals
9. rmdir Command
$ rmdir tutorials
Delete Empty Directory in Linux
If you try to remove a non-empty directory, you will get an error message as
shown.
$ rmdir projects
10. rm Command
$ rm filename
$ rm -R directory_name
$ rm -R projects
Sometimes, you may want to search the location of a particular file. You can
easily do this using either the find or locate commands.
The find command searches for a file in a particular location and takes two
arguments: the search path or directory and the file to be searched.
For example, to search for a file called file1.txt in the home directory, run:
$ locate filename
For example;
$ locate file1.txt
Locate Files in Linux
The locate command searches using a database of all the possible files and
directories in the system.
NOTE: The locate command is much faster than the find command. However,
the find command is much more powerful and works in situations
where locate does not produce the desired results.
That’s It! In this topic, we have covered the basic file management commands
that will give you the know-how in creating and managing files and directories in
a Linux system.