TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Linux

Linux: File Management

In Linux, almost everything is a file. This tutorial looks at creating and deleting directories and files. It also covers commands like copy and move to help with file management.
Jul 13th, 2024 9:00am by
Featued image for: Linux: File Management
Feature Image by Mariann Szőke from Pixabay.
This article on services fits into a larger series of Linux articles covering various sysadmin topics, including hardware identification and managing system processes. You can build a lab environment by following the information in the Linux: Companion Lab for Linux Skill Blocks Repository article.  In this series, we also covered how to pick a distribution, how the Linux kernel interacts with hardware and how Linux manages system services, storage, and user and group permissions.

Directories (also called folders) allow users to organize files. You would expect to find resumes and other job search materials in a directory named resumes. The Linux OS also uses directories to manage operating system files.

Most users create and manage files that store various types of information. From sales reports to favorite movies, files contain the specific data we all need.

This tutorial looks at creating and deleting directories and files. It also covers commands like copy and move to help with file management.

You can work with the file management commands found in this section without any additional setup, but you may find it useful to review the Understand the Linux Command Line and the recent User and Group Management. Users must enter a name and password combination to log on to the system to create, delete, and maintain files, so understanding how to manage accounts is handy.

That article specifies three new users: fsmith, slee, and mgarcia. It also creates three groups: IT, HR, and PR. I’ll reference these throughout this article, so you may want to quickly create the users and groups using the useradd and groupadd commands.

Note: It is a poor security practice to log on to a Linux system as the root (administrator) user. Most systems force you to log on as a regular user and then use the sudo (super user do) command to elevate your privileges. You may be prompted for your password when using sudo.

A follow-up article to this piece covers Linux permissions, which is how to control access to the files you create here for the users managed in the User and Group Management tutorial.

Refer to this tutorial if you’d like to build a lab environment to practice these commands.

Navigate the Directory Structure

You must know how to move or change from one directory to another. In a graphical user interface, you double-click on folders to browse into them. In a command line environment, you issue commands to navigate from directory to directory. The cd (change directory) command moves you from one folder to another.

For example, to move to a directory named /etc, type:


You can check your location in the folder structure at any time by using the pwd command:


Figure 1: Using the cd and pwd commands.

Take a few minutes to navigate through some directories yourself. Use the cd /media  and cd /bootcommands to practice navigating. Enter the pwd command to check your location.

Most user activities take place in the user’s account’s home folder, which is found inside the /home directory. Suppose you’re logged in as a user named fsmith. Your home directory is probably at /home/fsmith. If you’re not sure which account you’re logged in as, type the whoami command.

Figure 2: Using the whoami and pwd commands.

After you practice navigating through some directories using cd and pwd, type cd /home/fsmith (or whatever user name you have).

Linux includes many features to make your life easier. One is using the ~ character (look in the upper left corner of your keyboard) to represent the path to your home folder. For example, if your home directory is /home/fsmith, you could just type cd ~ to get there (instead of typing the whole path).

Type the ls command to display the contents of the directory. This shows you any directories or files inside the current directory. You may see folders like Documents, Downloads, Music, and others on your system.

Be careful of Linux’s case sensitivity. Directory names, file names, commands, and even options are all case-sensitive. You must get these values correct.

Commands covered in this section:

Command Description Example
cd Change from one directory to another cd /etc
~ Represents the current user’s home directory cd ~
pwd Shows the current location in the directory tree pwd
whoami Shows the username for the current user whoami
ls Lists the contents of a directory ls

Create and Delete Directories

There are two commands for managing directories: mkdir and rmdir. The mkdir command creates a new directory and the rmdir command removes existing directories.

Create Directories

Type cd ~  to confirm you’re in your home directory. Create some directories to organize business documents for a mock organization. Begin by using the mkdir command to create a directory named departments.


Figure 3: Using mkdir to create the departments directory.

If you use the ls command, you should see the departments folder listed as a subdirectory of your home directory. The idea is to simulate department-specific directories for a mock company.

Change to the departments folder using the cd command. Use the pwd command to confirm the change.


Figure 4: Changing to the departments directory.

Using the same command, create subdirectories in the departments folder named hr_dept, pr_dept, and it_dept. Confirm they exist by using the ls command.

Figure 5: Creating three subdirectories in the departments directory.

Delete Directories

Deleting directories is equally straightforward. Note that to use the rmdir command, the directory must be empty of files and subdirectories.

Create a new folder called test in your home directory and use ls to verify it exists.

Next, type the following command to delete an empty directory named test :

$ rmdir test

Note that some Linux distributions prompt for confirmation before deleting and some do not.

Commands covered in this section:

Command Description Example
mkdir Create a new directory mkdir 2024_sales
rmdir Delete an existing empty directory rmdir 2024_sales

Create and Delete Files

Directories are a way to organize files. The next logical step is to create some files. Don’t concern yourself with writing any real information in the files right now. This article focuses on files as objects and how administrators manipulate them.

Create Files

The touch command creates an empty file in a specified location. It’s a great way to quickly create a file to work with during exercises like this one.

Return to your home directory for this step using the cd ..  command (that’s cd, a space, then two dots).


The cd ..  command moves you one directory higher in the file structure. Your pwd should be /home/fsmith.

Use the touch command to create an object named fileA.txt  in your home directory (not one of the department directories created earlier).


Use the ls command to verify that fileA.txt exists in your home directory.

Figure 6: Using touch to create a file.

Remember that Linux is case sensitive, therefore, fileA.txt is different from filea.txt.

Use the cd command to change to the departments directory you created earlier. Change to the it_dept  directory next. The pwd command should show /home/fsmith/departments/it_dept.

Use the touch command to create a file named password-reset.txt in the it_dept directory.


You’ve now created some IT documentation in the it_dept directory! Feel free to create other files, such as laptop-inventory.txt or learning-resources.txt.

Figure 7: Using touch to create files.

Type cd .. to move up one directory in the structure. Your pwd should be /home/fsmith/departments. Change to the hr_dept directory and create a file named policies.txt. Using the same steps, change to the pr_dept directory and create a file named press-releases.txt.

Figure 8: Adding files to the various department directories.

Feel free to create additional files to practice working with the touch command.

Delete Files

The touch command creates files, but what about deleting files? Use the rm command to delete files from the system.

Change to your home directory by typing cd ~ and create a file named delete-me by using the touch command.

Delete the delete-me file using the rm command:


Carefully practice using rm. Note that by default there is no “trash can” at the Linux command line. Anything you delete is actually gone and generally unrecoverable. As with rmdir, some Linux distributions prompt you to confirm you want to delete the file and others do not.

Commands covered in this section:

Command Description Example
touch Creates a new empty file touch fileA.txt
cd .. Moves your location up one directory in the tree cd ..
rm Deletes an existing file rm fileA.txt

Copy and Move Files

Creating and deleting files is certainly useful, but you will probably want to copy and move them, too. Use the following examples to practice the copy and move commands.

I’ll begin with a simple syntax rule to help you use both commands. The syntax of the copy and move function is “from here to there.” You’ll designate the source or original file first (the “from here” part), then the destination to copy or move the file to (the “to there” part).

Imagine using a sentence to conduct the action: “Move fileA.txt from here in the test directory to over there in the extras directory.”

The next two sections provide details.

Copy Files

The command to copy files from one location to another is cp. When integrated with the syntax explanation above, the command reads cp (from here) (to there) .

Create two directories in your home folder named test and extras. Copy fileA.txt  from test to extras (create the fileA.txt file using touch if it doesn’t already exist). Type:


The source or “from” part is fileA.txt  and the destination to copy the file to is extras. You should now have a duplicate of fileA.txt in the home and extras directories. Verify this with the ls command.

Figure 9: Copying fileA.txt to the extras directory.

Practice the cp command with some test files.

Move (and Rename) Files

The mv (move) command doesn’t create a file duplicate like cp does. Otherwise, the syntax is the same — move from here to there.

Use touch to create fileB.txt and then move fileB.txt to extras by typing:


Figure 10: Moving a file to the extras directory.

Pay attention to the difference between copy (which creates a duplicate of the file in a specified location) and move (which actually moves the file to a specified location).

Interestingly, Linux doesn’t really have a specific “rename” command. Instead, you move the file from the current location to the current location with a new name. Confusing? It’s actually simple.

To rename fileA.txt to fileZ.txt, type:


Figure 11: Using the mv command to rename a file.

Use the ls command to confirm the rename function worked. Create and rename a few files for practice.

Commands covered in this section:

Command Description Example
cp Copy (duplicate) a file to a specified location cp fileA.txt /projects
mv Move a file to a specified location mv fileA.txt /projects
mv Rename a file mv fileA.txt fileZ.txt

Wrap Up

File management is a critical daily skill for Linux users. You’ll use directories to organize similar types of resources, such as music files or business documents. Your resources will be files you write using text editors, files you’ve downloaded, or maybe even programs you’ve written. Knowing how to create, move, copy, and delete files is essential.

Practice these skills until they become second nature. Don’t forget to use cd and pwd to navigate from directory to directory. Your next step should be learning how to use standard Linux permissions to control access to files and directories for specific user accounts and groups.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.