Unix and Linux directory management commands

last updated in Categories File system, Linux, UNIX

We use DNS (domain name system) to translate between domain names and IP addresses. For example, one can use the dig command/host command for DNS lookup on a Linux and Unix-like systems. Similarly, Linux files are referred by file names, not by inode number. So what is the purpose of a directory? You can group the files according to your usage. For example, all configuration files are stored under /etc/ directory. So the purpose of a directory is to make a connection between file names and their associated inode number. Inside every directory, you will find out two sub-directories named:


  1. . (single period) – The current directory
  2. .. (double period) – The pointer to previous directory i.e. the directory immediately above the one I am in now. The ‘..‘ appears in every directory except for the root directory. The ‘..‘ always points to the same inode as ‘.

Use the ls command to list files and directories including . and .. directories on Linux or Unix:
ls -la

Understanding the . and .. directories
Find the . and .. directories on Linux or Unix-like systems using the ls command

Directory

A directory contained inside another directory is called a sub-directory. At the end the directories form a tree structure. Use the tree command to see directory tree structure:
$ tree /etc | less

Linux and Unix See Directory Tree Structure
Use tree command to list contents of directories in a structure like format

Again a directory has an inode just like a file. It is a specially formatted file containing records which associate each name with an inode number. Please note the following limitation of directories under ext2/3 file system:

  • There is an upper limit of 32768 subdirectories in a single directory
  • There is a “soft” upper limit of about 10-15k files in a single directory
  • Ext4 and other modern Linux file systems allows an unlimited number of subdirectories

However according to official documentation of ext2/3 file system points that using a hashed directory index (which is under development) allows 100k-1M+ files in a single directory without performance problems. Here are my two favorite bash shell alias commands related to directory :
alias ..='cd ..'
alias d='ls -l | grep -E "^d"'

Unix and Linux directory management commands

A list of standard Linux/Unix commands to work with directories and files:

Command Description Example(s)
mkdir command Creates a new directory mkdir dir1
rmdir command Deletes the specified directory if it is already empty rmdir dir1
cd command Change the current directory cd /etc/
cd .. command Go back to previous directory cd ..
pwd command Display name of current/working directory pwd
cd command cd without any parameters changes to the user’s home directory cd
mv command Copies source to target then deletes the original source mv dir1 dir2
cp command Copies source to target cp -r dir1 /path/to/dir2
rm command Removes the specified files from the file system. Directories are not removed by rm unless the option -r is used rm file1
rm -r dir1
ln command Creates an internal link from source to target ln -s /etc/hosts /tmp/link
chown command Transfers ownership of a file to the user with the specified username chown userName file
chgrp command Transfers the group ownership of a given file to the group with the specified group name chgrp dir1
chmod command Changes the access permissions chmod 0444 dir1
locate command The locate command can find in which directory a specified file is located locate file1
find command Search for a file in a given directory find $HOME -name "hello.c"
file command Detect the contents of the specified files file /etc/resolv.conf
cat command Displays the contents of a file cat data.txt
less command See the contents of the specified file less resume.txt
more command It is a filter for paging through text one screenful at a time more /etc/hosts
grep command Finds a specific search string in the specified files grep "nameserver" /etc/resolv.conf
egrep command Same as grep but extended regular expression supported egrep -i 'err|cri|warn|' /var/log/messages
diff command Compares the contents of any two files diff old.c new.c

I am sure all of you know the basic commands related to directories and files management. Click the link on the left column to learn more about each command and same usage.

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

5 comment

  1. Thanks alot.This article is very useful for my project call “Describe the files and directory model in Linux?UNIX”

  2. Hi,

    Thx for the useful information abt the ext2 filesystem. What I want to find out is, whether any website is avbl for finding the detailed structure of a superblock ? i.e., all the fields that the superblocks stores, their valid contents, when are they changed, how r they affected when a file is changed etc…
    Thanks a lot in advance.

    -Swapnil

  3. Heya,

    the correct command to go back to previous directory (as seen in table graphic) should be
    cd –
    not
    cd ..

    cd .. go to parent directory
    cd – return to previous directory

    Have a question? Post it on our forum!