Linux Command Line – Part 2

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.

Other Parts


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 (bin, usr, var, etc, dev, 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 Windows, Program Files, 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.

Directory Description
/ Root directory of the entire file system hierarchy.
/bin Essential command binaries for all users. e.g., ls, cp, mv, etc.
/boot Files required for booting e.g., /boot/vmlinuz is compiled Linux kernel, /boot/initrd is temporary file system image containing the binaries, drivers to boot the system, /boot/grub/grub.conf is configuration file of boot loader.
/dev Contains list of all devices that kernel understands. Specially take a look at /dev/null device which discards any data passed to it.
/etc Contains system-wide configuration files and scripts required to start/stop services (/etc/init.d).
/home Contain home directories of all user accounts. Home directory usually contain user files (Music, Pictures, Documents, etc), and user specific system and application settings.
/lib Shared libraries (like dlls in Windows) required for proper functioning of binaries.
/media /media is relatively new directory added in FHS document and it’s now present in majority of modern Linux distros where it mount removable media like CD/DVD drives, different kind of USB storage devices, etc.
/mnt Used by the users to manually mount temporary filesystems.
/opt Reservered for installation of optional application software packages.
/proc /proc is a virtual filesystem (doesn’t exist on your hard drive) that stores kernel and process information as readonly text files.
/root Home directory of root user.
/sbin Contain system binaries that require superuser privileges to run.
/tmp Temporary files created by different programs which are often removed on system reboots.
/usr Contains user utilities, applications, and doc files. This directory is similar to Program Files in Windows.
/usr/bin All binaries for user programs goes in this directory.
/usr/include Include files (header files used by C/C++ programs) for user programs.
/usr/lib Shared libraries for the binaries in /usr/bin/ and /usr/sbin/.
/usr/sbin Non-essential system administration related binaries. Essential binares were in /sbin.
/usr/share Shared data required by binaries in /usr/bin e.g. icons, sound files, documentation files (/usr/share/doc) etc.
/usr/local Locally installed programs that are used by all users of the system just like the programs in /usr. Typically the programs which are not installed by the Linux distribution goes here. This directory further contains similar directories like bin, sbin, share, include, etc
/var Contain variable files that continually change e.g. logs, spool files, and temporary files.
/var/log Contain log files.
/var/run This directory contains system information files describing the system since it was booted. This directory is cleared on each reboot.
/var/spool Contains data which later require processing, e.g., print queues.
/var/tmp Temporary files that are preserved between reboots.

Above table is created with the help of FHS 2.3 document.

Mac OS X is based on Darwin and the Filesystem Hierarchy in Darwin looks pretty similar to that of Linux. OS X users must take look at this link for Filesystem Hierarchy.


Create

Files

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 abc.txt, xyz.txt. Also remember you can use both relative and absolute paths with almost any command.

Now you can start editing this file using vim, nano, gedit or some other editor. Check touch documentation using man command (man touch).

Directories

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 myDir on Desktop. Second one create three directories (dir1, dir2, dir3). Third one uses absolute path and it creates linux directory in my home directory ifadey.

Remember mkdir command creates a new directory only if it doesn’t exist. Check mkdir –help (two dashes before help) for more info.


Delete

Files

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.

Directories

Now create a new directory in your home directory using mkdir and try removing it using rm command.

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/

Useful Options

Force

Force option -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

Interactive

Interactive option -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.

Verbose

Finally verbose option -v is useful when deleting multiple files or directory containing multiple files. Verbose option will show you each file/directory that rm deleted.

ifadey@Inspiron-1564:~/Desktop$ rm -rv tstDir/
removed `tstDir/file1'
removed `tstDir/file3'
removed `tstDir/file2'
removed directory: `tstDir'



Copy

Files

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 /media directory.

Directories

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

Second example copy Pictures directory from current directory to Desktop directory.

Third example copy two directories from current directory to home directory. Note Both dir2 and 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.

Useful Options

Force

Force option -f with 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 article.odt from Documents to current directory.

Interactive

Interactive option -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 rm command.

Update

Update option -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 ./

Verbose

Verbose option -v again works in same way it did for rm command. When coping files, verbose option will show you each file/directory that cp copied.

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


Move

Files

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 mv command.

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.

Directories

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

Useful Options

Force

Force option -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 article.odt from Documents to current directory and will not prompt even if article.odt already exist.

Interactive

Interactive option -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

Update

Update option -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 ./

Verbose

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'

Rename File/Directories

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 dir1 to dir2.


Review

In this part you learned about the purpose of each folder in Linux (Filesystem Hierarchy Standard). Then you learned to create (touch and mkdir commands), delete (rm command), copy (cp command), and move (mv command) files and directories. Finally you learned to renames files/directories using mv command.


comments powered by Disqus