Linux Directory Commands : A Complete Guide (2024)

XFacebookLinkedIn

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.

Related:The Ultimate Guide to Windows Subsystem for Linux (Windows WSL)

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:

Linux Directory Commands : A Complete Guide (1)

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
Linux Directory Commands : A Complete Guide (2)

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
Linux Directory Commands : A Complete Guide (3)

Related:A Windows Guy in a Linux World: Users and File Permission

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

Linux Directory Commands : A Complete Guide (4)

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
Linux Directory Commands : A Complete Guide (5)

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"
Linux Directory Commands : A Complete Guide (6)

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"
Linux Directory Commands : A Complete Guide (7)

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
Linux Directory Commands : A Complete Guide (8)

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
Linux Directory Commands : A Complete Guide (9)

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 directorypwd# Change to the /var/log directorycd /var/log# Verify that you are now in the /var/log directorypwd
Linux Directory Commands : A Complete Guide (10)

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 directorypwd# Move up a single directorycd ..# Verify that you have moved up to the /var directorypwd
Linux Directory Commands : A Complete Guide (11)

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 directorypwd# Change to the /var/log directorycd log/# Verify that you are back in the /var/log directorypwd

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

Linux Directory Commands : A Complete Guide (12)

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.

Linux Directory Commands : A Complete Guide (13)

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.

Linux Directory Commands : A Complete Guide (14)

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.

Linux Directory Commands : A Complete Guide (15)

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.

Linux Directory Commands : A Complete Guide (16)

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.

Linux Directory Commands : A Complete Guide (17)

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
Linux Directory Commands : A Complete Guide (18)

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.

Linux Directory Commands : A Complete Guide (19)

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}
Linux Directory Commands : A Complete Guide (20)

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/
Linux Directory Commands : A Complete Guide (21)

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/
Linux Directory Commands : A Complete Guide (22)

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).

Linux Directory Commands : A Complete Guide (23)

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

Linux Directory Commands : A Complete Guide (24)

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_Renamedmv Desktop/MyAwesomeLinuxDir ~/MyAwesomeLinuxDir_Renamed
Linux Directory Commands : A Complete Guide (25)

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.

Linux Directory Commands : A Complete Guide (2024)
Top Articles
Sef2 Lewis Structure
Pipestone County Sheriff Custody List
Mansfield Shower Surround
F2Movies.fc
Target Dummies 101 - The Dummy Research/Tutorial Thread
Nene25 Sports
Urbfsdreamgirl
50 budget recipes to feed a large crowd
Best Restaurants In Nyack On The Water
Missed Connections Dayton Ohio
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
Guide to Gold Farming in Guild Wars 2 - MMOPIXEL
8 Casablanca Restaurants You’ll Want to Fly For | Will Fly for Food
Rooms For Rent Portland Oregon Craigslist
Who should be in the Country Music Hall of Fame (but isn't yet)? Our picks
Restaurant Depot Flyer December 2022
Samanthaschwartz Fapello
Happy Clown Makeup Tutorial
Spaghetti Models | Cyclocane
Cindie's - Lafayette Photos
modelo julia - PLAYBOARD
Car Star Apple Valley
Insidekp.kp.org Myhr Portal
Unveiling the World of Gimkit Hacks: A Deep Dive into Cheating
NWS Southern Region Tropical Webpage
Drive Mad Yandex
Unveiling AnonIB: The Controversial Online Haven for Explicit Images - The Technology For The Next Generation.
Hendrick Collision Center Fayetteville - Cliffdale Reviews
Bryant Air Conditioner Parts Diagram
Wyze Recover Deleted Events
Riverwood Family Services
Waive Upgrade Fee
Rule 34 Supreme Court: Key Insights and Implications
Seats 3D Ubs Arena
Ichc's Wheat Ridge Family Health Clinic
Decree Of Spite Poe
Shih Tzu Puppies For Sale In Michigan Under $500
Cvs Newr.me
Mission Impossible 7 Showtimes Near Regal Bridgeport Village
GW2 Fractured update patch notes 26th Nov 2013
Booknet.com Contract Marriage 2
ARK Fjordur: Ultimate Resource Guide | Where to Find All Materials - Games Fuze
Fired Dies Cancer Fired Qvc Hosts
Csulb Atlas
Craigslist Lasalle County Il
Vcu Basketball Wiki
4215 Tapper Rd Norton Oh 44203
Battlenet We Couldn't Verify Your Account With That Information
Toldeo Craigslist
James in Spanish | Spanish to Go
Rust Belt Revival Auctions
Omgekeerd zoeken op telefoonnummer | Telefoonboek.nl
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6400

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.