Bash Files and Directories

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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.Hierarchical file system 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.Showing the current directory with pwd 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.List of the files and sub directories in the current directory.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.Showing a detailed information of files and directories

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:

  1. To change the directory backward, you can use .. with the cd. It will take you to the parent directory of the current one.Showing the previous directory with cd command

  2. If you want to move to the home directory, then you can use only the cd command.Moving to the home directories with cd

  3. To navigate back to the home directory, one can use the cd ~. The tilde indicates the user’s home directory.Back to the files and directories

  4. 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.Changing the current directory into a specific directory 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

  1. You can use touch, nano, and vim commands to create a new empty file. Here is an example command to create an empty file.

    touch mealtime

    A file has been created with touch command

  2. 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”.Creating a new file with cat command

    Here 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.
    Showing the new created file with cat commandIn the image, the newly created files have been shown using the ls command.

  3. 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.Create a new file with text editor 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.Saving the created text editor file

  4. To create a new directory use the command mkdir followed by the directory name.Create a directory with mkdir command

    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.Read the content of a file

Use the command wc -lwc <filename> to count the lines, words, and characters of files.Word,line and character count of a file 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>Remove a file with rm command

You can also use rm -d command to remove empty directories.

Copy Files and Directories

  1. 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.Copying a file

  2. 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.Copy of the directory with cp command in bash

    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”.Rename a file into a new oneThis 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.

The new name should be unique. Otherwise, if it matches with another file name that already exists, the command will overwrite the content of that file.

Searching for Files

  1. The locate command shows the location of a specific file. You can use locate with the file name like below:

    locate xorg.conf

    Locate a file with locate commandThis command searches for the file named xorg.conf in the whole file system.

  2. 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”
    EXPLANATION

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

    Find a file with find commandIn the output, all the files that match the input name have been shown.

    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:

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

  2. 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.Structure of the file permission If you use ls -l then you can view the existing file permissions.Showing the file permission of the absolute permission mode

    EXPLANATION
    Content/Text

    In 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 condition of file before changing the permission mode 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>After changing the file permission 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.

Rate this post
Afia Zahin Oishi

Assalamualaikum, I am Afia Zahin, completed my graduation in Biomedical Engineering from Bangladesh University of Engineering and Technology, currently working as a Linux Content Developer Executive at SOFTEKO. A high achieving professional with a strong work ethic and able to work in a team in order to consistently achieve my goal and build my skillset. Able to handle difficult problems with patience and swift decision-making. Read Full Bio

Leave a Comment