Join our community of software engineering leaders and aspirational developers. Always
stay in-the-know by getting the most important news and exclusive content delivered
fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter
in the past. Click the button below to open the re-subscribe form
in a new tab. When you're done, simply close that tab and continue
with this form to complete your subscription.
The New Stack does not sell your information or share it with
unaffiliated third parties. By continuing, you agree to our
Terms of Use and
Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!
We’re so glad you’re here. You can expect all the best TNS content to arrive
Monday through Friday to keep you on top of the news and at the top of your game.
What’s next?
Check your inbox for a confirmation email where you can adjust your preferences
and even join additional groups.
Follow TNS on your favorite social media networks.
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.
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:
$ cd /etc
You can check your location in the folder structure at any time by using the pwd command:
$ pwd
Figure 1: Using the cd and pwd commands.
Take a few minutes to navigate through some directories yourself. Use the cd /media and cd /boot commands 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.
$ mkdir 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.
$ cd departments
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).
$ cd ..
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).
$ touch fileA.txt
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.
$ touch password-reset.txt
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:
$ rm delete-me
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:
$ cp fileA.txt extras
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:
$ mv fileB.txt extras
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:
$ mv fileA.txt fileZ.txt
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.