GuidesA Beginner's Guide to Linux File Management

A Beginner’s Guide to Linux File Management

In Linux, files are arranged in a hierarchical structure of directories or folders. A directory is simply a container for your files. Directories can further have subdirectories. The Linux file structure spans in a manner similar to branches of a tree and is hence commonly referred to as a tree structure. It begins with a root directory, which is referenced using /, or forward slash, on the terminal. The root directory contains a number of system directories and files that govern your Linux system.

In this guide, developers and sysadmins will learn how to interact with files and directories in Linux using the command line.

Linux Command Line Tutorial
Example of Linux Command Line Courtesy

How to Display File Content in Linux

First, you need to know the files in the directory you are currently in. To list the files in your current directory, use the ls command:

$ ls

To display the contents of a given file to your terminal window, you can use the cat command, followed by the file name. For example:

$ cat filename1

How to Manage Linux Users from the Terminal

Copying File Content in Linux Directories

There are some instances where you may need to copy a file to another directory (or even the same one). For this, use the cp command. The syntax is:

$ cp [source] [destination]

The source indicates the filename of the file to be copied. In case you are working from a directory different from the file, you can instead indicate its file path. The destination is the directory to which you would like to save the copied file:

$ cp filename1 /home/joel/

The above command creates a copy of filename1 and then saves it to the directory /home/joel/ with the same file name.

You also have the option to save the file with a different name. Simply add the new filename at the end of the file path, as shown below:

$ cp filename2 /home/joel/filename3

See the example below on how you can make a copy of a file to the same directory:

$ cp filenameX filenameY

How to Manage Linux Groups Using Command Line

Moving and Deleting Files in Linux Directories

Moving a file refers to shifting it to a different directory. This is different from copying a file, since the original file is removed from its initial directory. To move a file, use the mv command, followed by the filename name and its destination directory:

$ mv filenameX /tmp

It’s interesting to note that the mv command can be used to rename a file. The example below renames file3 to file4:

$ mv file3 file4

To delete a file, use the rm command, followed by the filename. You may need administrative privileges. If prompted to do so, prefix the rm command with sudo.

$ rm filename2

Directory Management in Linux

As mentioned earlier, files are stored in directories. In most operating systems, a directory is commonly referred to as a folder. To create a directory, type the mkdir command followed by the directory name and run the command:

$ mkdir folder1

You may want to know your present working directory. Simply run the pwd command in you terminal to do so:

$ pwd

To list the contents in your current directory, run the ls command.

$ ls

To change your current directory, use the cd command followed by the directory you would like to change to.

$ cd /etc

In case you want to delete a directory, use the rmdir command followed by the directory name.

$ rmdir /tmp/folder1 

If the directory you are deleting has sub-directories, your system will flag an error when you run rmdir. To recursively delete folders, add the -r option.

You may also want to move a folder into another folder. Use the syntax below to achieve this:

$ mv folder1/ folder2/ 

If you want to copy all the file contents in a folder, you will still use the cp command, though, you will need to add the -R option:

$ cp -R  folder1/ folder2/

Summary of File Management in Linux

Directories are used for organizing files in your Linux system. When creating your own directories and subdirectories, it is only fair that your arrangement is logical while doing so (since directories are meant to help you organize files and other directories). Keep this in mind when tackling the task of managing your files in Linux.

Featured Partners: File Sharing Software

Zoho Assist

Visit website

With Zoho Assist File transfer, you can send and receive files and folders during your remote sessions. Effortlessly transfer and manage files as large as 5 GB in size with no maximum limit for a single transfer. Files of all formats can be shared without dependence on intermediate storage, like USBs or cloud-based storage devices, keeping your workflow uninterrupted and your data protected.

Learn more about Zoho Assist

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories