Files and directories are fundamental components of the file system that can be interacted with through command-line operations in Bash. For the effortless management of files and directories in bash, one must know about the navigation of files, file and directory manipulation, and the file system permission. Let’s see how to handle files properly.
File System in Linux
Linux provides a hierarchical file system that helps users to store, organize, and manage files efficiently. When one creates a file, the location depends on the directory in which it has been created. By default, it is located in the current working directories when the path is not specified. From the image, you can see the root directory is at the top of the hierarchical system and denoted as a forward slash(/). Under the root directory, all the files are organized.
Navigating File System
Navigation is the process of moving through the file system and determining and reaching a particular destination. For navigating, one can use “cd”, “ls”, and “pwd” commands.
Show the Current Directory
To see the current directory you can use pwd
command. It will show the directory where you are currently working.
List All Files in a Directory
If you want to list the files of the current directory then use ls
which will let you see a list of the files and directories.Here, ls stands for list which shows the list of the working directory that you are currently in.
For a detailed view of files and directories to see the file type permission, owner, group, file size, modification tile, and file name you can type the ls -l
command.
If you want to see the file list including the hidden files with detailed information then you can use the ls -la
command.
Change Current Directory
The cd
command is used to change the current directory. Here are its common applications:
-
To change the directory backward, you can use
..
with thecd
. It will take you to the parent directory of the current one. -
If you want to move to the home directory, then you can use only the
cd
command. -
To navigate back to the home directory, one can use the
cd ~
. The tilde indicates the user’s home directory. -
If you want to navigate a specific path then you have to type cd followed by the path of the directory (cd <path>). For example, to navigate to the /Desktop directory from the home directory, use the following command:
cd /home/<user name>/Desktop
Replace <user name> in the above command with the username of your Linux. Here /home/oishi/Desktop is the absolute path. While using the path always use “/” and as Linux is case sensitive, make sure to use accurate capitalization for files and directories.
File and Directory Management Operations
To maintain a secure, organized, and efficient system, file and directory management is very important. Let’s see some essential guides:
Creating Files and Directories
-
You can use
touch
,nano
, andvim
commands to create a new empty file. Here is an example command to create an empty file.touch mealtime
-
Another method to create a file is using the cat command with redirection. It is a quick method to create and modify a file. For example,
cat > filename
will create a file named “filename”.In the image, the newly created files have been shown using theHere in the place of filename, you can use your preferred file name. But note that, if the file name already exists then the timestamp will be updated without modifying the content inside the file.ls
command. -
You can also create a file using a text editor. For example, use the nano command following the file name and press ENTER. This will create an empty file in the name given to it. To write out something or save the edit to the file, press CTRL+O and then ENTER to save the changes, after that press CTRL+X to exit.
- To create a new directory use the command
mkdir
followed by the directory name.Choose a unique name for the directory. If the directory name already exists in your current working directory, then it will show an error instead of modifying or overwriting.
How to Read a File
If you want to read a file then you can use the cat
command with the filename. For example, cat colors.txt will show the content of the “colors.txt” file of your current working directory.
wc -lwc <filename>
to count the lines, words, and characters of files. In the output, it is shown that there are 13 lines, 11 words, and 74 characters in the text file. Removing Files and Directories
To delete a file, (rm <filename>) and to remove a directory, use rm -r <directory name>
rm -d
command to remove empty directories. Copy Files and Directories
-
To copy a file to a new directory, use the
cp
command with the file name that you want to copy and the directory name where you want to copy. For example,cp demo2 demo4
will copy the content of demo2 to demo4. - To copy directories, use the
cp -r
command followed by the directory name that you want to copy and the destination directory. For example,cp -r
white yellow will copy the contents of the white directory to the yellow directory.However, the original copy of the file will not moved, now there are two copies of the same file and directories.
Rename Files and Directories
To rename a file or directory name, use the mv
command followed by the old and new filename. For example, mv oldname newname will rename the “oldname” file to “newname”.This command is also used to move a file and directories. For example, mv demo2 dekoo
will move the contents of demo2 to dekoo directory. In this case, the original copy of the file demo2 will be removed.
Searching for Files
-
The
locate
command shows the location of a specific file. You can use locate with the file name like below:locate xorg.conf
This command searches for the file named xorg.conf in the whole file system.
-
The
find
command is used to search for a file with name, type, size, permission, and modification time. Here I have searched for a file with the find command:find . -name “colors.txt”
In the output, all the files that match the input name have been shown.EXPLANATIONIn this command “.” indicates the current directory. Here, you can specify the absolute path of the file. Here
-name
option is used to search the desired file.In this command, you can also use wild characters like this,*.txt
which will match and show all the files which are end with “.txt” in the current directory.
File Permission
File permission is an important part of the system security that determines who can access, modify, and execute the file. The authorization of a file is divided into two parts:
- Ownership
The files are classified into three types of owners which are User, Group, and others. The user is the owner who mainly created the file. When multiple users of a group use the same permission to access a file then it is called group owner. When anyone who has not created the file and is not in a user group, has permission to access a file then it refers to set permission for the world.
- Permission
The permission for all the files in the system is divided into three parts which are Read(r), Write(w), and Execute (x) permission. If you use the command
ls -l
then you can see this type of file type and excess permissions. If you usels -l
then you can view the existing file permissions.EXPLANATIONContent/TextIn
ls -l
the “-” indicates the file, and “d” indicates the directory. In the section, “-rw-rw-r–”, the first “-” indicates it is a file. Then “rw-” indicates the user’s permission, the later part of user “rw-” indicates the group permission, and the last part “r–” indicates others’ permission.
Change File Permission
To change file permission, use the chmod
command which refers to change mode. Modes can be changed in any of the two forms – absolute mode and symbolic mode.
In the symbolic mode, you can change the file permission for a specific owner at a time. On the other hand, for absolute mode, you can change the file permission for all the owners.
Here is an example of the absolute mode permission. The output indicates that the current permission for the file is read & write for the user and group and only read permission for others.
Let’s change the user permission into read+write+execution and group permission into write+execution. It can be done with the command chmod 734 <filename>
From the image you can see the chmod command which is the full form is the change mode, changes the file permission.
Conclusion
Understanding files and directories is essential for navigating and manipulating the file system using command-line tools in Bash. In this article, the basics of handling the files and directories have been shown using the appropriate commands which is very essential for the beginner.
People Also Ask
Can I use a special character with the ls command?
Yes, you can use the special character like * with ls. For example, if you want to list the files with a “.txt” extension then you can use the command ls *.txt and it will show all the files in the current working directory.
How does a file system organize data?
The file system of Linux organizes files into a hierarchical structure. In this type of structure, all the files and directories are organized under the directories and form a tree-like structure which helps the users to navigate the files properly.
How do I list the directory other than the current directory?
To list the directory you can use the ls command with a tilde (~). For example, if you want to list the files on the desktop then use the command ls ~/Desktop
. Also, you can use this command when the directory is in the home directory but for any other directory, you can use the basic command ls /path/specified-name.
Can I display the number of matching files?
Yes, you can display the number of the same entries with the locate -c
command. For example, to see the number of files that have the name “colors.txt”, use the command locate -c colors.txt
.
FUNDAMENTALS A Complete Guide for Beginners