Managing Files and Directories in Linux

Manage Files and Directories in Linux

Did you know managing files and directories linux command line is much easier than you thought and once you get used to with the command line you can manage files and directories much quicker compared to any graphical user interface! In this tutorial we are going to learn how we can easily create, read, edit, move, rename and delete files and directories in Linux using the command line. We know the commands may vary depending on the OS you are using but it doesn't matter if you use CentOS, Ubuntu, Fedora, RockyLinux, AlmaLinux or any other Linux Distribution, the following commands are universal and going to work on all Linux Systems.

Table of Contents

1. Print Working Directory : SSH pwd command

The pwd command stands for Print Working Directory which is used to determine your current location in the file system. When you are connected to the server over SSH you won't have a graphical user interface so you have to relay on the command line and whenever you type pwd on SSH Terminal it will print the name of your working directory. e.g.

Print Working Directory in Linux SSH

In the above example my working directory is /root

2. Listing Files in a Directory : SSH ls command

The ls command stands for Listing and used to list files or directories in Linux and other Unix-based operating systems. If you want to see the list of your files and directories under your working directory, type ls and press Enter.

Listing Files in Linux SSH

As you are seeing ls command has printed one file on my current working directory. I have more files there but rest of them are hidden and ls command with no option flag will print only the visible files. To list all files and directories including hidden ones enter ls command again with -la flag. e.g. ls -la

Listing All Files Including Hidden in Linux SSH

The working directory wasn't changed. After entering ls command with -la flag, now I can see all files and directories including the hidden ones.

3. Change Directory : SSH cd command

cd stands for Change Directory and you can use cd command to navigate from one directory to another.

  • Please remember the first slash / indicates the path is relative to the root directory. e.g cd /home/ Here / represents your root directory and home is a directory placed in your root directory. When you need to navigate to other directories placed inside a directory just type cd directoryname You shouldn't use the starting slash / when you will be accessing other directories inside a directory. Or if you want to start your cd command with a slash / you should write the full path to the directory as follows :
1cd /path/to/directory

Changing Directory in Linux SSH

Syntax Description
cd dir_name Change directory relative to your working directory
cd /path/to/dir_name Change directory relative to your root directory
cd ../ Navigate to the parent directory of the current working directory
cd ../../ Navigate up two levels in the directory structure.
cd ~ Navigate to the user’s home directory.

4. Creating Directory : SSH mkdir command

To create a new directory type mkdir followed by a space and the name of the directory. e.g.

1mkdir testdir
  • The above mkdir testdir command will create a directory named testdir in your working directory.
  • mkdir {test1,test2,test3} command will create 3 directories in your current working directory with the names test1, test2 and test3.
  • mkdir command with a -p flag followed by a space and then the full path to the new directory will create all the parent directories even if they are missing. Suppose you have /home/documents/ this empty directory with no other file or directory there. If you need to create the directory order_copies at /home/documents/office/order_copies/ this location you can either create all the directories one by one or just type the command as follows:
1mkdir -p /home/documents/office/order_copies/

The command above will create the order_copies directory with all missing parent directories.

Creating Directory in Linux SSH

Syntax Description
mkdir dir_name Creates a directory in the current location
mkdir {test1,test2,test3} Creates multiple directories in the current location
mkdir -p /path/to/new/directory Creates directories even if the parent directories are missing

5. Creating Files : SSH touch command

In Linux the touch command allows you to create a new file with or without any extension.

Creating File in Linux SSH

  • To create a single file on your working directory type
1touch filename.txt
  • If you want to create multiple files at once type
1touch file1.log file2.txt

The command above will create two files with names file.log and file2.txt in your working directory.

  • Or if you want to create the file not in your working directory but at some other location, issue the following command :
1touch /path/to/your/file

6. Reading File Contents : SSH cat command

Concatenate or in short cat command is one of most frequently used commands in Linux. It can be used to display the content of a file, copy content from one file to another, concatenate the contents of multiple files.

In this lesson we will only see how we can read the file contents using cat command.

  • The command for showing the contents of a file in SSH Terminal is :
1cat file_name

Example :

Reading File Contents in Linux SSH

7. Editing files Using nano or vi Editor

Nano and vi both are terminal-based text editor developed for Unix and Linux operating systems. Both of them are simple, lightweight and easy to use. For beginners Nano is relatively easy to learn compared to vi but vi is very rich in features and capabilities. vi may look a bit complex when you will be using it for the first time but I'm certain it's going to be your favourite text editor once you get used to. In this section I will be discussing only on the basics on editing files in Linux using nano and vi editor.

Using Nano Editor

Basic Syntax:

1nano filename.ext

Replace filename.ext with your file name and extension. The command above will open your file showing the contents of the file or create an empty file if you don't have the file already created. You can start editing your files's contents directly from that screen and once you are done making changes press CTRL + X on your keyboard. You will be then asked if you want to save your modification or not. Press Y on your keyboard, then you will be asked to enter file name. Just press Enter to save the modification you just made.

Here's a video demonstration of editing file in Linux SSH using Nano editor Editing file in Linux SSH using Nano editor

Using Vi Editor

Basic Syntax:

1vi filename.ext
  • The Vi editor has two modes: Command and Insert. When you first open a file with Vi, you are in Command mode.
  • Command mode means you can use keyboard keys to navigate, copy the file contents but you won't be able to write anything while you are in command mode.
  • To enter Insert mode, press i on they keyboard. If you see -- INSERT -- at the bottom of the vi editor, you successfully switched to the insert mode. In Insert mode you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate.
  • When you are done editing in insert mode press Esc key once to return to command mode again.
  • You can then type :wq from the keyboard and press Enter to save your edit and quit from vi editor.

Here's a video demonstration of editing file in Linux SSH using VI editor Editing file in Linux SSH using VI editor

Here's some basic vi commands:

Syntax Description
vi file_name Open the file with vi editor
i Switch to Insert mode
Esc Switch to command mode
:w Save and continue editing
:wq Save and exit
:q! Exit without saving

Learn More About Vi Editor

8. Copying files and directories : SSH cp command

  • The cp command is used to make a copy of your files and directories. The syntax is:
1cp [options] [source] [destination]
  • If we want to make a copy of our file in the same directory, the command would be as follows:
1cp filename.txt filename_backup.txt
  • If we want to make a copy of our file to the other location in the file system, the command would be as follows:
1cp filename.txt /path/to/directory/filename_backup.txt

You can also copy an entire directory along with all sub directories using the recursive flag -R e.g.

1cp -R directory/ /path/to/new/directory


Copying Files and Directories in Linux SSH

Here's some other options that you can use with the cp command.

Syntax Description
-i i stands for Interactive copying. If the file exists at the destination you will be asked if you want to replace the file or not.
1[[email protected] home]# cp -i filename.txt filename_backup.txt
2cp: overwrite 'filename_backup.txt'? y
3[[email protected] home]#
Syntax Description
-f f stands for force copying. If the file exists at the destination but with incorrect permission, the cp command may fail. In such cases you can use cp command with -f flag. The force option will delete the destination file first and then the file will be copied from source to destination.
1[[email protected] home]# cp filename.txt filename_backup.txt
2cp: cannot create regular file 'filename_backup.txt': Permission denied
3[[email protected] home]#
5#In such cases command should execute successfully with -f flag
7[[email protected] home]# cp -f filename.txt filename_backup.txt
8[[email protected] home]#
Syntax Description
* cp command with * option can be used to do wildcard copying. Suppose you have a lot of files inside a directory, some with .txt extension and some with .php extension. If you want to copy all .txt files from that directory to another directory you can easily do this with a single command.
1[[email protected] home]# ls
2file1.php  filename2.txt  filename_backup.txt  testdir
3file2.php  filename3.txt  filename.txt         testdir_2
4[[email protected] home]# ls testdir
5[[email protected] home]# cp *.txt testdir
6[[email protected] home]# ls testdir
7filename2.txt  filename3.txt  filename_backup.txt  filename.txt
8[[email protected] home]#

9. Moving/Renaming file and directory : SSH mv command

10. Deleting file and directory : SSH rm command