Linux file management

As we all know Linux is an operating system mostly used by Geeks and developers who are mostly keyboard people and love to write commands instead of using a Graphical user Interface (GUI). Unlike the Windows operating system, where most of the work done is with few clicks while in Linux, we have commands for everything like basic file manipulations, compression or extraction of files etc. These commands run on the Linux command line which is known as Terminal or Shell. The Terminal or shell is a utility in Linux which is responsible to run the commands.

Due to the larger data, tons of files are in a system. So, it becomes difficult for a user to find and manage a particular file. Usually, users take the help of GUI for managing a file. These commands provide flexibility to manage the files at a higher speed with much more options, and the user will get more meaningful results efficiently. In this tutorial, we will see different commands to manage files.

List files using ls command

“ls” is mainly used to list the files. “ls” command can be used with different options providing different outcomes. We will be using “ls” command to find the files.

Step 1: First of all, open Terminal by clicking on the Ubuntu launcher and search for Terminal.

Serach for the Terminal app

Step 2: Now click on the Terminal and wait for the terminal to open.

Open Linux Terminal

Step 3: Once the terminal is opened, you will have a screen like this:

Linux Terminal ready for use

Step 4: We can use the ls command in different ways in part 1, we used it without any option attached to it, to see the outcome. While the next two commands contain specific options to show file/ directory size and the other one to view the hidden files.

Listing Files using ls:

We use the ls command without any option, so here we will not be able to view the details of the file type, its size, and directory. We enter “ls”

List files with ls command

We get an outcome that is the list of files without any specific detail.

List result from ls command

Listing Files with option –l:

Here we have used option –l to see the details of the file. It includes the directory of the file, its size, modified date, time, details about the credentials, etc. we simply write the option as “ls –l”.

Linux ls -l command

We get an outcome that is the list of files with specific details about the file.

File list as shown when ls -l is used

Viewing the hidden files:

We can also find out a list of all hidden files. The hidden files start with “.”. We write “ls -a” to find out hidden files.

the -a command option of the ls command

The outcome shows a list of all hidden files.

Show hidden files on Linux

We can use any of the ways of listing based on user requirements.

Changing the directory using the cd command

The command “cd” is used for this purpose which allows a user to change the current folder or the directory. We can change the current directory by specifying a new directory in the cd command as “cd [directory-name]” e.g. here the new directory is “Desktop” so we write it as:

Linux cd command

Now the directory in which we are in Desktop.

Result of cd command

Further, if we want to move and change the directory to any other one like home, we will write the path “cd /home” of the new directory next to the current directory.

Using cd with / path

Now our current directory, which was Desktop previously, has been changed to “home” directory.

These commands can change the file directory.

Remove files using rm

Using rm command:

In order to remove/ delete a file, we need to be in the same directory where the file which we want to delete is located. The file we are going to delete is located at “home,” and the following is the method to change the directory. If we want to move and change the directory to any other one, like home, we will write the path “cd /home” of the new directory next to the current directory because our file “abc.txt” which we want to delete is located at the home directory.

Enter directory with cd command

Now our current directory, which was Desktop previously, has been changed to “home” directory.

We are currently in the /home directory

So, in order to remove the file, we use the command “rm abc.txt”, where abc.txt is the file of our interest. It could be any file based on our demand.

Delete file with rm command

The file “abc.txt” has now been removed from the home.

File list in Desktop file viewer

So, by this command, we can remove the file we desire to remove/ delete.

using rmdir command

We use the rmdir command when we want to remove an empty directory. We use the command “rmdir directoryname”. Here we have an empty directory named as “emptyDirectory” at home. We will remove this in this part of the tutorial.

Here is the “emptyDirectory” placed at home.

Create directory

The command used to remove the empty directory is rmdir directory name. Here the name of the directory is emptyDirectory.

Delete Directory

The empty directory has been removed.

Empty directory has been removed

Move files using mv

To move a file from one location to another, we use “mv” command. Here the file we want to move is sample.txt which is located on Desktop currently.

File list for our example

We will be using “mv” command to move the sample.txt file from the Desktop to the Downloads folder. For this purpose, we write the command “mv sample.txt /new directory”.

Move file with mv command on Linux shell

Here, we wanted to move our file to downloads. Therefore, the directory to downloads is “/home/bush/downloads”.

File successfully moved

By using the mv command, we moved our file to downloads.

Copy files using cp

We use the cp command to copy files to another directory. We will use the cp command to copy a file from source to destination. File.txt is the file we are copying to the desktop from the current home directory, in our case. The command is “cp file.txt /Destination”.

Copy file with CP command

If the destination to which we are copying the file already contains the same file then, it asks us to overwrite the file or not using “-i option”. The user is asked to overwrite the file or not.

Confirm command action

In case if we don’t want the file to be overwritten, then we use the option “-n”.

cp -n command

The above-mentioned commands are a few of the examples associated with cp command.

Make directories using mkdir

We will create a new directory named “abc” in the current working directory using “mkdir” command. For this purpose, we will write the command “mkdir newDirectory”, i.e. abc in our case, and we write it in the current work directory to create a new directory in the exact location.

Linux mkdir command

We will then go to the Desktop to see the newly created directory “abc”.

Directory created successfully

This is how we can create a new directory using mkdir command in the command line of the terminal.

Change file permissions using chmod command

To set the permission for a file and a folder, we can use the chmod command. Chmod uses the following symbolic representation for three different roles:

  • u is for user
  • g is for group
  • o is for others.

Following are few of the symbolic representations of three different permissions:

  • r is for read permission
  • w is for write permission
  • x is for execute permission.

The following example uses the chmod command to change the permission. Where the plus “+” sign means we are adding the permission. The following example gives the execute permissions to a user without even specifying anything else. So, the user is permitted to do the execution of all kinds. We use chmod u+x filename.

chmod u+x

We can also allow multiple permissions to a file/ directory. A comma is used to separate the multiple permissions. Here, g is for a group, x is again for execution, while u is for a specific user, and r is for reading. So, several permissions are allowed in one command. That is, a group of users can execute, and also a specific user can be permitted to read the file anytime.

Extended chmod example

We can also remove read and write permissions using the chmod “chmod u-rx filename” command as follows.

Another chmod example

It will remove the permissions to read and write in a specific file.

Create empty files using touch command

The touch command can be used to modify and change the access/modification timestamps of specific files. It is used to create an empty file quickly. It uses a simple touch command as follows.

Linux touch command

We just created a file abc.txt using this command.

Touch command result

The file, when opened, you can be is empty file abc.txt

Empty file created by touch command

Multiple files can also be created using the touch command. We created three files at a time using this command. The file names that we created are “abc.txt”, “cde.txt”, “xyz.txt”.

Create multiple files with the touch command

The three empty files have been created. In case a file already exists, its access time is updated.

Files successfully created with touch

Conclusion

In this tutorial, we have discussed a few commands for file management in the Linux terminal. At first, we discussed the commands for listing the files, then the commands to change the directory, and thirdly, we have seen the commands to remove the files/ directories. Then we observed how to copy and move the files from one location to another. The next part contains commands to make new directories. Later we discussed how to change the file permissions and how to create empty files in the Linux command line.

Linux File Management from the Terminal

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn