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:

ADVERTISEMENTS

  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:

CommandDescriptionExample(s)
mkdir commandCreates a new directorymkdir dir1
rmdir commandDeletes the specified directory if it is already emptyrmdir dir1
cd commandChange the current directorycd /etc/
cd .. commandGo back to previous directorycd ..
pwd commandDisplay name of current/working directorypwd
cd commandcd without any parameters changes to the user’s home directorycd
mv commandCopies source to target then deletes the original sourcemv dir1 dir2
cp commandCopies source to targetcp -r dir1 /path/to/dir2
rm commandRemoves the specified files from the file system. Directories are not removed by rm unless the option -r is usedrm file1
rm -r dir1
ln commandCreates an internal link from source to targetln -s /etc/hosts /tmp/link
chown commandTransfers ownership of a file to the user with the specified usernamechown userName file
chgrp commandTransfers the group ownership of a given file to the group with the specified group namechgrp dir1
chmod commandChanges the access permissionschmod 0444 dir1
locate commandThe locate command can find in which directory a specified file is locatedlocate file1
find commandSearch for a file in a given directoryfind $HOME -name "hello.c"
file commandDetect the contents of the specified filesfile /etc/resolv.conf
cat commandDisplays the contents of a filecat data.txt
less commandSee the contents of the specified fileless resume.txt
more commandIt is a filter for paging through text one screenful at a timemore /etc/hosts
grep commandFinds a specific search string in the specified filesgrep "nameserver" /etc/resolv.conf
egrep commandSame as grep but extended regular expression supportedegrep -i 'err|cri|warn|' /var/log/messages
diff commandCompares the contents of any two filesdiff 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.

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.


ADVERTISEMENTS

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

Leave a Comment