Linux file management

As we all know Linux is an operating system mostly used by the Geeks and developers which are mostly a keyboard person and love to write commands instead of using Graphical user Interface (GUI). Unlike 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 there 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 higher speed with much more options, and the user will get more meaningful result efficiently. In this tutorial, we will see different commands to manage files.

List files using ls command

“ls” is mostly 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 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 terminal is opened, you will have a screen like this:

Linux Terminal ready for use

Step 4: We can use ls command in different ways in part 1, we have 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 ls command without any option, so here we will not be able to view the details of file type, its size and directory. We just 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 an option –l to see the details of the file. It includes 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 simply 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 cd command

The command “cd” is used for this purpose that 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 is 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 new directory next to the current directory.

Using cd with / path

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

The file directory can be changed by these commands.

Remove files using rm

Using rm command:

In order to remove/ delete a file, we need to be in same directory where our file which we want to delete is located. The file we are going to delete is located at “home” and following is the method to change the directory. We want to move and change the directory to any other one like home, we will write the path “cd /home” of 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 that 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 which we desire to remove/ delete.

using rmdir command

We use rmdir command when 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 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 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 that is located at Desktop currently.

File list for our example

We will be using “mv” command to move the sample.txt file from Desktop to Downloads. 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 mv command we, moved our file to downloads.

Copy files using cp

We use cp command to copy file to another directory. We will use cp command to copy a file from source to destination. File.txt is the file we are copying to the desktop from the current directory that is home 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 few of the examples associated with cp command.

Make directories using mkdir

We will create a new directory named “abc” in the currently 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 current work directory to create a new directory in same 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

In order to set the permission for a file and a folder, we can use chmod command. Chmod uses 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.

Following example uses chmod command to change the permission. Where plus “+” sign means we are adding the permission. The following example is giving 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 group, x is again for execution, while u is for a specific user and r is for read. 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 chmod “chmod u-rx filename” command as follows.

Another chmod example

It will remove the permissions to read 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 see is empty file abc.txt

Empty file created by touch command

Multiple files can also be created using touch command. We created 3 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 files that are empty 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 few commands for the file management in Linux terminal. At first, we have discussed the commands for listing the files, then the commands to change the directory, 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 Linux command line.

Linux File Management from the Terminal