by

How to manage files from the Linux terminal

If you are a Linux administrator or a regular user, you should need to know the basics of managing files and navigating directories in your operating system. Whether it is a desktop or just a command-line based OS, both offer a simple and straightforward way for managing files and directories. However, in some cases, it is quicker to use the command line for performing basic management tasks or navigating in directories, especially when working with a large number of files. These commands offer flexibility to manage files at a quick speed with much more options.

In this article, we will explain how to manage files and directories using the Linux Terminal. We have used Debian 10 for describing the procedure mentioned in this article.

List files using ls command

The ls command is used to list files and the sub-directories under the current directory. It also provides some options that can be used to get additional information about the files.

First, open the Terminal by going into the Activities tab at the top left corner of your Debian desktop. Then in the search bar, type terminal. When the Terminal icon appears, click on it to launch it.

Listing Files using ls:

We use will ls command without any option, so here it will not show the details of file type, its size, and directory. So just type ls in the Terminal as follows:

$ ls

You can see in the following view the ls command has listed the file names without any specific detail.

list files

Listing Files with option –l:

Here we have used an option –l that will show the details of the file. It shows some details of the file, its size, permissions, modified date, time, etc. So simply type the following command in Terminal:

$ ls -l

You can see in the following view the ls –l has listed specific details about the file.

list of files on Linux with ls command

Viewing the hidden files

ls can also be used to list all hidden files. The hidden file names start with “.”. Type the following command in Terminal to list hidden files:

$ ls -a

From the output, you can view a list of all hidden files.

ls -la

Changing the directory using the cd command

The command “cd” allows to change the current working directory or in other words to navigate to another folder in your system. Simply type cd followed by the pathname of the desired directory.

$ cd <path/to/directory>

For instance, to navigate to Desktop, we will use the following command:

$ cd Desktop/

In the following output, you can see that the working directory has changed to Desktop.

cd Desktop

Further, if we want to navigate to and change the directory to any other one like home directory, you will have to add the path “cd /home”.

$ cd /home

Now you can see that the current directory has been changed to “home” from the Desktop/.

cd /home

Remove files using rm

The rm stands for remove as the name implies is used for removing or deleting files and directories in Linux OS. However, you have to be careful with this command as it will not ask for confirmation before deletion.

Using rm command:

To remove/ delete a file, navigate to the directory where the file to be deleted is present. In the following example, we are going to delete a file named file1.txt located at Documents folder under the home directory. So first navigate to the desired directory using the cd command and then type rm followed by the filename to remove the file.

$ cd /home/tin/Documents/
$ rm file1.txt

remove file on Linux

Using rmdir command

The rmdir command is used to remove/delete empty directories. If the specified directory is containing a file or sub-directory, then it will not be deleted using the rmdir command.

Navigate to the location where the empty directory is located. Then type rmdir followed by the directory name as follows:

$ rmdir <directory_name>

In the following example, we are removing an empty directory named myfiles under the Documents directory using the following commands:

$ cd /home/tin/Documents

$ rmdir myfiles/

Remove directory on Linux

Move files using mv

mv stands for move. This command is used to move one or multiple files or directories from one location to another in Linux OS. The general syntax of the command is:

$ mv <source> <destination>

In the following example, we are going to move a file named file1.txt that is currently located at Downloads directory. We want to move it to the Documents directory.

$ mv /home/tin/Downlaods/file1.txt /home/tin/Documents/

Move files with mv command

Copy files using cp

The cp command is used for copying files and directories. We will use the cp command to copy a file from source to destination. The general syntax of the command is :

$ cp <options> source destination

In the following example, file1.txt is the file we are copying to the Documents directory from the current directory that is Downloads directory in our case. The command would be:

$ cp /home/tin/Downloads/file1.txt /home/tin/Documents/

Copy files with cp command on Linux

During copy, if the destination file already exists, it will be over written. To prompt for the confirmation, use the –i option. It will ask the user whether to overwrite the file or not.

$ cp -i /home/tin/Downloads/file1.txt /home/tin/Documents/

If you do not want the file to be overwritten, then use the option “-n”.

$ cp -n /home/tin/Downloads/file1.txt /home/tin/Documents/

Make directories using mkdir

The mkdir command is used to create a new directory at the current working directory. The general syntax of the command is:

$ mkdir <directory_name>

In the following example, we are creating a new directory named softwares in the current working directory using the mkdir command as follows:

$ mkdir softwares

Create a directory with mkdir command

Change file permissions using chmod command

The chmod command is used to set the permission for a file and a folder. Each file and directory is assigned three types of owners that are represented by u, g and o:

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

Following are the permissions defined for all the above owners that are represented by r, w, and x:

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

In the following example, we are using chmod command to change the permission. Plus “+” sign means to add the permission. For instance, to give a user the execute permission, use the following command in Terminal:

$ chmod u+x files1.txt

So, now the user is allowed to do the execution of all types.

change user with chmod

We can also allow multiple permissions to a file/ directory. A comma is used to separate the multiple permissions as follows:

$ chmod u+r ,g+x file_name

Change user and group with chmod

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

$ chmod u-rx file_name

remove write permission using chmod

Create empty files using the touch command

The touch command is used to create empty files. It can also be used to create, change, and modify timestamps of the file. The general syntax of the command is:

$ touch file_name

In the following example, we are creating an empty file named file1.txt using the touch command.

Create empty file on Linux using touch command

By using the touch command, we can also create multiple files. In this example, we are creating 3 empty files at a time using the following command:

$ touch file1.txt file2.txt file3.txt

Create multiple file using touch command

When the file already exists, its access time will be updated.

In this article, we have learned different commands for file management in Debian Terminal. While there are a lot more commands to learn about file management but these are basics to start with. I hope it would be helpful whenever you need to do basic navigation or file management in your Linux OS.

Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

News Feed