Navigating and manipulating files in Linux is a core concept for fluently operating the system. The terminal acts as your powerful control center, allowing you to traverse through directories, fetch files, and perform modifications. Whether you work on a local Linux installation or access a remote system via a secured SSH session, you can rely on basic commands to streamline your tasks.
This tutorial will use practical examples and precise instructions to demonstrate Linux file navigation and manipulation.
Linux structures its files and directories in a hierarchical layout resembling a tree, where the root directory (/
) serves as the foundation. Everything, including folders and files, branches out from this root. Below is a concise synopsis of some key directories:
It is the main folder where everything begins. Every other directory and file is kept inside it.
This folder contains personal files for every user. For instance, if your username is hostman
, files will be kept in /home/hostman
.
This folder keeps frequently updated files, including system logs and temporary data. Logs can be located in /var/log
.
Linux preserves configuration files and system settings, including networking and account setup inside the etc directory.
Navigating and organizing files involves traversing the file system to handle files and directories. Linux commands make these tasks efficient, let you automate them, and provide you precise control, especially in non-graphical environments.
The pwd
command retrieves your current address in the file system. It's useful for identifying your position when navigating through complex directory structures:
pwd
The outcome indicates that the active directory is anees
located inside the system’s home directory:
The cd
utility lets us traverse distinct directories. Entering the command below will take us to the root directory:
cd /
Let’s employ cd
with the tilde symbol ~
or cd
without any option to traverse back to the Home Directory:
cd ~
Execute cd
with the -
sign to toggle between current and previous directories:
cd -
It switches us from hostmanExamples
to the previous folder, i.e., hostman
:
Similarly, running cd
with a precise path lets us access a particular file/directory:
cd Desktop/hostman/hostmanExamples
ls
is a Linux utility that retrieves the folder’s data, such as files, links, and sub-folders. You can utilize distinct flags with ls
to exhibit additional details like access rights, sizes, and last modified timestamps.
Let’s utilize ls
without any flag to retrieve the folder’s data from the recent path:
ls
Utilizing ls
with a precise path retrieves the details of that directory:
ls Desktop/hostman/
To get invisible files, utilize ls
with -a
flag:
ls -a
To demonstrate precise information, run ls followed by the -l
flag:
ls -l
The tree
command retrieves folders and files in a hierarchical tree format. To do that, utilize the syntax:
tree /directory_path
Let’s retrieve the directory structure in a tree format, with each file’s size depicted in a human-understandable structure:
tree -h
These files or folders initiate with a dot (.
) and are invisible by default. They typically keep configuration settings or important data for applications, such as .bashrc
for customizing terminal behavior or .ssh
for managing secure keys. They can be viewed by utilizing the ls -a
command or by enabling the "show hidden files" from the file managers.
Let’s study the below-listed Linux sections to handle files and directories effortlessly.
Files are made in Linux via the touch
command. It offers a simple method to make blank files. If the file is already present, touch
revises the DateTime
of the last change instead of generating a new file:
touch hostmanExample.txt
The mkdir
utility in Linux allows you to create a directory at the desired path:
mkdir hostmanFolder
It constructs a hostmanFolder
in the current directory:
mv
lets you alter the name of files or transfer them to a distinct location. To re-title a file, write the existing name followed by the desired one:
mv hostmanExample.txt hostman1.txt
This alters the name of hostmanExample.txt
to hostman1.txt
:
Likewise, users can transfer a file by specifying its name and the destination directory:
mv hostman1.txt /hostmanFolder
To duplicate a file/folder, execute cp
with the source file and the destination path:
cp graphqlFile.txt hostmanFolder
To duplicate a directory along with its contents, employ the -r
(recursive) flag with cp
:
cp -r graphql-examples hostmanFolder
It clones the complete graphql-examples
folder with all its data to the hostmanFolder
:
The rm
command removes files and directories eternally without sending them to the bin/trash, so use it cautiously by providing the file name:
rm graphqlFile.txt
Likewise, to trash a directory and all of its data, employ the -r
flag:
rm -r hostmanFolder
For extended safety, utilize the -i
flag, which asks for verification before deleting any item:
rm -ri graphql-examples
Specify y
(for "yes") and press Enter to approve the removal of each file or directory. If you input n
, the file or directory will not be deleted.
Zipping files is vital for sharing data, and organizing files and storage space. To accomplish this, Linux utilizes tools like gzip
and bzip2
.
gzip
is a widely employed tool for compressing files. It decreases the file size while retaining the original data. Let’s employ gzip
to compress the hostmanExample.txt
file:
gzip hostmanExample.txt
The command compresses the original file (overrides the actual file):
Similarly, users can execute gzip
with -d
flag to decompress a compressed file:
gzip -d hostmanExample.txt.gz
Users can employ the -k
flag to compress a file without overriding the original one:
gzip -k hostmanExample.txt
File archiving refers to the method of grouping multiple files and directories together into one unified archive file. This practice makes it easier to share, compress, and manage large data by consolidating various items into a single, organized package. For this purpose, we can utilize the tar
command:
tar -cvf hostman.tar hostmanExample.txt hostman1.txt hostmanDir/
By default, it makes an archive without compression, which contains hostmanExample.txt
and hostman1.txt
files and a hostmanDir
directory:
To assemble a compressed archive, we can define the compression format such as z
for gzip
and j
for bzip2
:
tar -czvf hostman1.tar.gz hostmanExample.txt hostman1.txt hostmanDir/
Similarly, users can extract the archived data by executing the tar
command with the -x
flag:
tar -xvf hostman.tar
To extract a gzip
or bzip2
-compressed archives, use the -z
or -j
flags respectively:
tar -xzvf hostman1.tar.gz
Shortcut keys can save time and make command-line navigation more efficient:
cd -
to switch to the previous directory.cd ..
to steer one directory up.cd ~
to return to the home folder.Gaining proficiency in these fundamental shortcut keys will simplify and enhance your experience with Linux file management.
In this write-up, we wrapped the essential techniques for navigating and handling files in Linux. We examined how to switch between directories and depict their data. We also examined the creation, deletion, renaming, and relocation of files and directories. Additionally, we explored invisible files, their functionality, and methods to handle them. Mastering these core skills will make working with Linux more easily and effectively.