Your Guide to Managing Directories and Files in Linux [Step by Step]

Bill Kindle

Read more posts by this author.

Linux skills are always in demand. Build your skill-set by learning how to use Linux directory commands and Linux file commands. This guide is written as a journey. A set of step-by-step instructions guiding you through navigating, creating, removing, moving, renaming directories and files. All from the Linux command line.

If you are ready to build practical, real-world Linux skills, you’ve come to the right place!

Prerequisites

To practice the Linux examples in this guide, you must have a computer running Linux. The distribution of Linux does not matter. All demos in this guide will be using RedHat Enterprise Linux 8 (RHEL).

Ubuntu 20.04 LTS is a great alternative if you don’t want to use RedHat.

Listing and Finding Directories

If you are following along, open a terminal session on your Linux computer.

Finding the Current Directory With pwd

When you’re navigating a file system in a terminal, your prompt is always in a certain directory called a current directory or working directory. Depending on the terminal configuration, you may or may not see the working directory in the prompt.

The pwd or print working directory command displays the current directory you are in. Enter the command as shown below:

pwd

By default, the /home/<username> directory is your starting point in a terminal session unless you are signed in with the root account.

When you run the command, the working directory is returned as shown below:

Display the current working directory path.
Display the current working directory path.

Unlike Windows, Linux does not use drive letters (C:\, D:\, etc.) for disks or partitions (a logical region on a disk). In Linux, directories are preceded by a /.

Listing Files and Folders With the ls Command

Inside of directories, you’ll find files and subfolders. The pwd command just displays the working directory so how do you see subdirectories and files? The ls command.

The ls or list directory command is the equivalent to the dir command in Windows. This command lists files and directories in the current directory or any alternative path specified.

The dir command exists in Linux as well. Equivalent to running ls -C -b. Typically, ls is used over dir as more display flexibility exists.

To find files and directories in the /home/user directory, run the ls command. The resulting output is the various files and directories (folders) contained within.

ls
List contents of the current directory.
List contents of the current directory.

The ls command also displays additional information about files and folders such as permissions, file size (in bytes), and file dates with the l option. Like in Windows, some files and folders may be hidden by the OS from normal view. View files that are not shown by default with the a option.

Dotfiles are plain text configuration files in Unix systems. Any file can be hidden when the filename is preceded with a dot ..

ls -la

You can see in the below screenshot by using the la options, you receive a lot more information displayed in six different columns from left to right:

  • Permissions, in the POSIX ACL format, with the preceding d signifying a directory
  • Subdirectory count with directories containing only files as 1
  • User and group that a file or folder belongs to
  • Directory or file size in bytes
  • Last modified time
  • Name of the file or folder
Display a long list of all folders and files in current directory.
Display a long list of all folders and files in current directory.

You can find all of the files and folders in sub-directories with the -R option to display directory contents recursively.

ls -R
Recursively displaying contents of a directory.
Recursively displaying contents of a directory.

If you want to display the contents of a different directory, pass the target directory to ls. Shown below are the /var/log directory contents without leaving the /home/user directory.

ls /var/log
List the contents of a specific directory.
List the contents of a specific directory.

Locating Files and Folders with find

Locating files across a large file system is difficult. Thankfully, the find command offers several ways to locate the files and folders you may need.

For example, you want to find the /var directory. Given a starting point, /, and setting the type as d for directory, search the filesystem for the var name.

sudo find / -type d -name "var"
Using the find command to look for all directories that contain the word "var".
Using the find command to look for all directories that contain the word “var”.

You may see a Permission Denied error even when you run the command using sudo. The find command attempts to search a virtual file system related to Gnome (If you use KDE, this error won’t apply) that your user doesn’t have access to. Instead, don’t descend directories on other filesystems by using the-xdev switch instead.

How about finding all files with the log extension? Similar to before, search the root filesystem, but this time specifying f for files and search for the wildcard pattern of *.log.

sudo find / -type f -name "*.log"
Searching for all files ending in the .log extension.
Searching for all files ending in the .log extension.

The find command has other options, such as H, L, and P, which handle how symbolic links are treated.

Searching for Files Faster with locate

Like find, the locate command searches for files and folders but does so faster by searching a database instead of the entire filesystem.

Maybe you have a file named MyTextFile.txt somewhere on your computer. You’ve forgotten where it’s located and need some help finding the path. Use the locate command to find the file, providing it the name of the file as shown below.

locate MyTextFile.txt
Locating MyTextFile.txt on the filesystem.
Locating MyTextFile.txt on the filesystem.

The locate command finds files but will not find new files until sudo updatedb runs. A cron job runs updatedb once a day, but you can run this command manually at any time.

How would you find all directories named tmp? You could pass */tmp, which tells locate to search the database for entries that end in tmp.

locate */tmp
Search for all folders that end in tmp.
Search for all folders that end in tmp.

Changing Directories with the cd Command

Up until now, you have operated within a single directory for the most part. Operating in Linux often means changing directories, done through the cd or change directory command. Using the example below moves from the current home directory, /home/user, to /var/log while verifying your current directory.

# Show that you are in the current home directory
pwd
# Change to the /var/log directory
cd /var/log
# Verify that you are now in the /var/log directory
pwd
Change directories in Linux.
Change directories in Linux.

Now that you have learned to change to a specific directory, how do you move up a directory? To move up a directory, you will use two dots (..), passed to the cd command. Once run, as seen below, you move up a single directory, putting you back in the /var/ directory.

# Show that you are currently in the /var/log directory
pwd
# Move up a single directory
cd ..
# Verify that you have moved up to the /var directory
pwd
Moving up a single directory.
Moving up a single directory.

When you want to change into the /var/log directory again, pass the directory name to change to with a trailing /.

# Show that you are currently in the /var directory
pwd
# Change to the /var/log directory
cd log/
# Verify that you are back in the /var/log directory
pwd

As seen below, only entering the destination sub-directory with a trailing / removes the need to enter an absolute path every time.

Move to a sub-directory.
Move to a sub-directory.

If you are unsure of a file or directory name when using cd, press the TAB key twice to produce a list of possibilities. Example: cd <TAB> <TAB>

Making a New Directory With mkdir

Creating a directory in Linux is through the mkdir or make directory command. To create a new directory called MyAwesomeLinuxDir in your home directory (noted by the special path ~), use the command below.

mkdir ~/MyAwesomeLinuxDir

If Linux creates the directory successfully, Linux will not return a message in the console. You can list the directories in your home directory with the ls command to verify that the directory, MyAwesomeLinuxDir, exists.

Creating a directory in Linux.
Creating a directory in Linux.

Now add some complexity and create multiple new directories with a single command. To create multiple directories, you use the mkdir command and pass multiple directory names separated by a space.

mkdir ~/Directory01 ~/Directory02 ~/Directory03

Once again, list the directories with the ls command. As you can see below, three more directories exist now.

Creating multiple directories with mkdir.
Creating multiple directories with mkdir.

As useful as using mkdir to create a single folder is, what if you need to create many folders all at once? Instead of typing each directory out, use brace expansion! This technique is powerful when creating multiple directories with a similar pattern.

Brace expansion makes creating multiple directories based on a pattern quicker than if typing each by hand. To demonstrate brace expansion, append {03..07} after the name Directory to generate five directories. You may notice that Directory03 already exists because you created it in a previous example. Avoid an error about existing directories with the p option.

mkdir -p ~/Directory{03..07}

As seen below, Directory03 is not created, but four new directories ending in 04 thru 07 now exist.

Creating multiple directories with brace expansion
Creating multiple directories with brace expansion

Creating a New File With touch

Now that you have learned to create folders, how do you create a file? Using the Linux touch command create an empty file, as seen in the next example.

touch ATABlog

Once again, list the files and folders in your home directory, to see that the new ATABlog file exists.

Creating a file with the touch command.
Creating a file with the touch command.

Remember how you learned that the mkdir command can use brace expansion to create multiple directories? The same technique is useful with the touch command as well! Create five more ATABlog files ending 01 through 05.

touch ATABlog{01..05}

After listing the files and folders with ls, five new files exist in your home directory.

Creating multiple files with brace expansion and the touch command.
Creating multiple files with brace expansion and the touch command.

Removing Directories and Files in Linux

In the last section, you learned how to create directories and files in Linux. To remove directories and files, use the rm command to remove directories and their contents.

For example, you want to remove a file called ATABlog01. Remove a file by passing the file name to the rm command. As seen below, you are removing the ATABlog01 file from the current directory.

rm ATABlog01
Removing a file in Linux.
Removing a file in Linux.

You can remove multiple files at once by adding a space between each filename in the current directory. Delete the remaining ATABlog## files in a single rm command, as shown below.

rm ATABlog02 ATABlog03 ATABlog04 ATABlog05

Now that you have learned to remove files, it’s time to remove a directory. By default, the rm command does not remove directories. Remove a directory by specifying the r or recursive option, as seen in the below example.

rm -r Directory01

In the screenshot below, you can see that Directory01 was removed.

Removing directory via the recursive command of rm.
Removing directory via the recursive command of rm.

You can use brace expansion with the rm command too. Providing the 2 to 7 range within the rm command informs the remove command to delete all directories named Directory02 through Directory07. As shown when confirmed by an ls command.

rm -r Directory{02..07}
Removing multiple directories using rm and sequencing
Removing multiple directories using rm and sequencing

To prevent accidentally removing the wrong files or folders use the i to prompt for each file. Make the option less onerous with the I option which only prompts on three or more files.

Copying Directories and Files in Linux

Copy files in Linux with the cp command. Not only can the cp command copy directories and files in Linux, but also file attributes and creating symbolic links.

To demonstrate copying a file from the previous examples, copy the ATABlog file to the Documents/ directory with the cp command. Add the v option to display more information about the exact copy operation.

cp -v ATABlog Documents/
Copy a file in Linux with the cp command.
Copy a file in Linux with the cp command.

Now, copy the folder, MyAwesomeLinuxDir, to the Documents directory. Here, as with the rm command, be sure to add the r or recursive option to copy the directory, as seen in the below example.

cp -r MyAwesomeLinuxDir/ Documents/
Copy the folder using the recursive option.
Copy the folder using the recursive option.

Move Directories and Files

In the previous section, you copied files and folders using the cp command. To move directories and files in Linux, use the mv command. To move directories in Linux, use the mv command. The mv command is similar to that of the cp command you learned about in previous examples.

mv [options] [source] [destination]

The mv command is like cutting and pasting in Windows, with a bonus of being able to rename files simultaneously. The next sections will show you how to use mv for both scenarios.

Use the -i option to prompt before each move or the -f option to forcefully move items without prompting.

To demonstrate, move the file Documents/ATABlog to the Desktop directory with the mv command. Move the folder, Documents/MyAwesomeLinuxDir to the Desktop as well, as shown below.

mv Documents/ATABlog Desktop/
mv Documents/MyAwesomeLinuxDir Desktop/

Your commands should look like the example below. Remember, you are moving the file and directory (source) to another directory (destination).

Using the mv command to move files and folders
Using the mv command to move files and folders

Use the ls command to see that both are now located in the Desktop directory.

Moving a file and folder in Linux.
Moving a file and folder in Linux.

Renaming Directories and Files

Although there is not a rename command, the mv command plays the same role. Instead of only passing a destination to the mv command, specify the resulting file or folder without a trailing slash to move and rename.

Move the file and folder back to your home directory while renaming the file to ATABlog_Renamed. Similarly, do the same with the MyAwesomeLinuxDir, and rename the directory to MyAwesomeLinuxDir_Renamed in the home directory, as shown below.

mv Desktop/ATABlog ~/ATABlog_Renamed

mv Desktop/MyAwesomeLinuxDir ~/MyAwesomeLinuxDir_Renamed
Moving files and folders with mv command
Moving files and folders with mv command

Next Steps

In this article, you have learned many of the common Linux directory commands, such as how to traverse a Linux filesystem along with creating, moving, and deleting files.

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!