In first part you learned about navigating file system using Bash shell and you also learned how to get help for a particular command. In this part you will learn:
- Filesystem Hierarchy Standard.
- Create, Copy, Move, and Delete files or directories.
- Rename file or directories.
- Part 1
- Part 2 (current)
Filesystem Hierarchy Standard
In previous part you learned to navigate file system but what if you don’t know where to find the files you want or in other words what is in different directories (
home, and many more) that exist in root of the file system?
Let me give you an example of Windows so you understand what I mean by Filesystem Hierarchy. In Windows all of your partitions (C, D, E, etc) are mounted in
Computer. Typically Windows is installed in C drive and with in that drive you find some important directories like
All Users, etc. You may know that
Windows directory contains the important system files of the OS.
Program Files is where user programs are installed. Similarly
All Users contain the home directories of all user accounts. So it make up a tree like structure and regular user of any OS understand what different folders/directories contain in this tree structure that exist on the file system. This tree like structure is actually Filesystem Hierarchy.
The good news is that file system hierarchy for Linux is standardized and this standard is known as FHS (Filesystem Hierarchy Standard). Modern distros have tried a lot to get compliant with this standard but still not all of them are 100% compliant with it. So you may find some variations in your Linux distro. Here’s a high level view of FHS.
||Root directory of the entire file system hierarchy.|
||Essential command binaries for all users. e.g., ls, cp, mv, etc.|
||Files required for booting e.g.,
||Contains list of all devices that kernel understands. Specially take a look at
||Contains system-wide configuration files and scripts required to start/stop services (
||Contain home directories of all user accounts. Home directory usually contain user files (Music, Pictures, Documents, etc), and user specific system and application settings.|
||Shared libraries (like dlls in Windows) required for proper functioning of binaries.|
||Used by the users to manually mount temporary filesystems.|
||Reservered for installation of optional application software packages.|
||Home directory of root user.|
||Contain system binaries that require superuser privileges to run.|
||Temporary files created by different programs which are often removed on system reboots.|
||Contains user utilities, applications, and doc files. This directory is similar to
||All binaries for user programs goes in this directory.|
||Include files (header files used by C/C++ programs) for user programs.|
||Shared libraries for the binaries in
||Non-essential system administration related binaries. Essential binares were in
||Shared data required by binaries in
||Locally installed programs that are used by all users of the system just like the programs in
||Contain variable files that continually change e.g. logs, spool files, and temporary files.|
||Contain log files.|
||This directory contains system information files describing the system since it was booted. This directory is cleared on each reboot.|
||Contains data which later require processing, e.g., print queues.|
||Temporary files that are preserved between reboots.|
Above table is created with the help of FHS 2.3 document.
There’s no specific command for creating a file but you can achieve this task using
touch command. Actually
touch command is used to modify timestamps of file. But the file will automatically gets created if it doesn’t exist. Here is an example which creates a text file on Desktop.
ifadey@Inspiron-1564:~/Desktop$ touch abc.txt
You can also create multiple files by passing more than one argument.
ifadey@Inspiron-1564:~/Desktop$ touch abc.txt xyz.txt
Above command will create two files
xyz.txt. Also remember you can use both relative and absolute paths with almost any command.
Now you can start editing this file using
gedit or some other editor. Check
touch documentation using
man command (
Creating directory is pretty simple task.
mkdir (MaKe DIRectory) is the command used to create directories. Here are some examples.
ifadey@Inspiron-1564:~/Desktop$ mkdir myDir ifadey@Inspiron-1564:~/Desktop$ mkdir dir1 dir2 dir3 ifadey@Inspiron-1564:~/Desktop$ mkdir /home/ifadey/linux
First command creates
Desktop. Second one create three directories (
dir3). Third one uses absolute path and it creates
linux directory in my home directory
mkdir command creates a new directory only if it doesn’t exist. Check
mkdir –help (two dashes before help) for more info.
rm (ReMove) command is used for deleting both files and directories. Here’s how you can delete a file.
ifadey@Inspiron-1564:~/Desktop$ rm someFile.txt
CAUTION: Remember that
rm command doesn’t move files/directories to Trash. It deletes them permanently. So be careful when using it.
You can use both absolute and relative paths with rm command. Here are some more examples.
ifadey@Inspiron-1564:~/Desktop$ rm file1 ../file2 /home/ifadey/file3
Above command removes three files. First from present working directory (which is
Desktop in above example). Second file (using relative path) from parent directory (which is
ifadey) and third one from the same directory but using absolute path.
Now create a new directory in your home directory using
mkdir and try removing it using
ifadey@Inspiron-1564:~$ mkdir newDir ifadey@Inspiron-1564:~$ rm newDir/ rm: cannot remove `newDir/': Is a directory
As you can see
rm was unable to remove a directory and gave an error “Is a directory”. It’s because you need
-r option to delete a directory and its contents recursively. Here’s how you can delete the directory.
ifadey@Inspiron-1564:~$ rm -r newDir/
-f, when used won’t give you error “No such file or directory” if file or directory doesn’t exist. It’s useful option specially when you write scripts.
ifadey@ifadey-Inspiron-1564:~$ rm -f fileNotExist
-i, when used will prompt you before removing file or directory.
ifadey@ifadey-Inspiron-1564:~$ rm -ir someDir rm: remove directory `someDir'? y
Type y for yes and n for no. Note that two options interactive and recursive (
-ir) are used together in above command.
Finally verbose option
-v is useful when deleting multiple files or directory containing multiple files. Verbose option will show you each file/directory that
ifadey@Inspiron-1564:~/Desktop$ rm -rv tstDir/
removed directory: `tstDir'
-vis useful when deleting multiple files or directory containing multiple files. Verbose option will show you each file/directory that
ifadey@Inspiron-1564:~/Desktop$ rm -rv tstDir/ removed `tstDir/file1' removed `tstDir/file3' removed `tstDir/file2' removed directory: `tstDir'
cp command is used to copy files and directories.
cp command accept list of files/directories which you want to copy and then the destination directory as final argument. Here are some examples.
ifadey@Inspiron-1564:~$ cp Desktop/tst.htm . ifadey@Inspiron-1564:~$ cp readme.txt script.php /var/www ifadey@Inspiron-1564:~$ cp /var/www/launch.py /media/myStorageDevice
In first example,
tst.htm file is copied from
Desktop to current working directory. Note the arguments in the first command. First argument (SOURCE) is
Desktop/tst.htm which is a relative path and refers to
tst.htm file in
Desktop directory which is in current directory. Second argument (DESTINATION) is . (dot) which represents current directory.
Similarly in second example, two files are copied from current directory to
www directory. In this example first two arguments are the source files. In short multiple sources are allowed and destination must be single directory.
Final command uses absolute paths to copy
launch.py (Python script file) from
www directory to some external storage device.
NOTE: In modern Linux distributions, external devices (CD/DVD Discs, USB HDD or SSD, etc) are automatically mounted in
Copying directories is similar to copying files with one exception and i.e. recursive option
-r is required to copy directories just like
rm command required it for deleting directories. Rest of the rules are same as for copying files.
ifadey@Inspiron-1564:~$ cp -r dir1 copy_of_dir1 ifadey@Inspiron-1564:~$ cp -r Pictures Desktop ifadey@Inspiron-1564:~$ cp -r dir2 dir3 /home ifadey@Inspiron-1564:~$ cp -r /home/ifadey/Downloads/books eBooks
First example copy
dir1 in the same directory (current directory) but with different name
Second example copy
Pictures directory from current directory to
Third example copy two directories from current directory to
home directory. Note Both
dir3 are relative paths (relative to current directory) and
/home is absolute path.
Final example uses absolute path to copy
books directory to
eBooks directory (relative path) in current directory.
cp command have similar important options as
rm command have.
cp command works bit differently than it did in
rm command. By default
cp command overwrite existing files but if it’s not able to overwrite any file and
-f option is used then it will remove the existing file and try to copy the new one. Here’s an example:
ifadey@ifadey-Inspiron-1564:~$ cp -f Documents/article.odt ./
Above command copies
Documents to current directory.
-i will prompt before overwrite.
ifadey@ifadey-Inspiron-1564:~$ cp -i examples.txt dwhelper/tst cp: overwrite `dwhelper/tst'? y
This option works exactly in same way as it did for
-u is used for copying only when the SOURCE file is newer than the destination file or when the destination file is missing.
ifadey@ifadey-Inspiron-1564:~$ cp -u Documents/article.odt ./
-v again works in same way it did for
rm command. When coping files, verbose option will show you each file/directory that
ifadey@Inspiron-1564:~/Desktop$ mv -v dir ../ `dir' -> `../dir'
Above command moves
dir directory from
Desktop to parent directory and display details after performing its operation because of verbose option
mv command is used to move files around file system and it works in a similar way as
cp command did. The only difference is that
mv command removes the SOURCE files after completing its operation (just like Cut/Paste in GUI). Here are the same examples from copying files section but with
ifadey@Inspiron-1564:~$ mv Desktop/tst.htm . ifadey@Inspiron-1564:~$ mv readme.txt script.php /var/www ifadey@Inspiron-1564:~$ mv /var/www/launch.py /media/myStorageDevice
Above commands as same as in copy section so I am not going to explain the arguments used with them.
Moving directories is also same as copying except with one variation.
mv command doesn’t require recursive option
-r to move directories.
ifadey@Inspiron-1564:~$ mv dir1 copy_of_dir1 ifadey@Inspiron-1564:~$ mv Pictures Desktop ifadey@Inspiron-1564:~$ mv dir2 dir3 /home ifadey@Inspiron-1564:~$ mv /home/ifadey/Downloads/books eBooks
-f when used with
mv command will not prompt before overwriting existing file.
ifadey@ifadey-Inspiron-1564:~$ mv -f Documents/article.odt ./
Above command moves
Documents to current directory and will not prompt even if
article.odt already exist.
-i is opposite to
-f option. It will prompt before overwriting.
ifadey@ifadey-Inspiron-1564:~$ mv -i examples.txt dwhelper/tst mv: overwrite `dwhelper/tst'? y
-u will move only when the SOURCE file is newer than the destination file or when the destination file is missing.
ifadey@ifadey-Inspiron-1564:~$ mv -u Documents/article.odt ./
Remember verbose option
-v works almost same with every command. It purpose it to simply display details regarding each option currently performed.
ifadey@Inspiron-1564:~/Desktop$ mv -v nodeServer/ ../ `nodeServer/' -> `../nodeServer'
Another important use of
mv command is to rename files and directories. Here’s an example for renaming file.
ifadey@Inspiron-1564:~/Desktop$ mv tst.htm index.htm
Above command renames
tst.htm file to
index.htm. Following example is for renaming directory.
ifadey@Inspiron-1564:~/Desktop$ mv dir1 dir2
Above command renames
In this part you learned about the purpose of each folder in Linux (Filesystem Hierarchy Standard). Then you learned to create (
mkdir commands), delete (
rm command), copy (
cp command), and move (
mv command) files and directories. Finally you learned to renames files/directories using