Common directory and file management commands in Linux

Directory and File Management in Linux
This entry is part 5 of 8 in the series Network Admin in SSH

Directory and File Management in LinuxThe 5th one in the series (with around 5 more left), and this time we are going to talk about common directory and file management tasks. It is pretty easy to manage files, create directories etc in a GUI based browser, but while managing servers, we are given a terminal. Instead of our favorite windows browser (or nautilus, or dolphin), we are left with a shell which responds to some specific keywords. Let us now see how we can use the shell to do a few common file and directory management. At the end of this, you should be able to:

  • Find your way around inside the shell.
  • Create, move, rename, delete directories.
  • Create, move, rename, delete files.
  • Bulk copy, move files and directories.

Let’s start now.

#1: Location and searching:

#1.1: Finding your way around:

This is the most important and, perhaps, most easy thing. While working with console (or terminal), most of the commands are executed on the current working location (if not otherwise mentioned), so it becomes important to know your working location before executing any command.

This is basically called present working directory and can be printed on the terminal window by executing the pwd command. It works on your location, gets it absolute path and prints it on the screen.

pwd

Output:

/home/swashata

#1.2: Understanding the relative and absolute path:

The path of a file or directory can be relative or absolute. The primary difference between the synopsis is:

  • Absolute path starts with a /
  • Relative path does not.

So, if we are to represent the abc file located inside the root level home directory > user directory, then path will be

/home/user/abc

Whereas, if the same file resides inside the present working directory, it can be represented by

abc

while working inside the said directory.

Let us consider we are working under /home/user directory. Now we want to write the path of public_html directory under it.

Relative path:

public_html

Absolute path:

/home/user/public_html

If we want to refer inside the directory, then we append a front-slash after it.

public_html/

or,

/home/user/public_html/

Although, you might think it is not needed, but it becomes helpful to check whether the path is of  a file or a directory. In Linux file system, everything, including directories, are treated as a file. So, you can not have a file and a directory at the same path with the same name.

#1.3: Listing the contents of a directory:

We can simply use ls command.

Print the directories/files of the pwd:

ls

Print the contents of some other directory:

ls path/to/dir

Print file permissions (long listing format):

ls -l

Print hidden files & directories (starting with .):

ls -a

Printing everything inside the root directory with permissions:

ls -al /

For other directories, replace / with the path of the directory.

#2: Common File/Directory Operations:

Common file and directory operations involve, creating, moving, deleting and renaming files and directories.

#2.1: Creating a file:

Creating a file involves invoking a terminal based text editor. There are many, but common are vi and nano. I personally prefer nano, as it is light-weighted and simple to use.

Creating a file with nano:

Start the  nano editor with the file-name as command line argument.

nano hello.php

This will give you an editor screen like this. Create and edit the file.

Working with NANO editor in Linux
Working with NANO editor
Linux Command Line

To save it press Ctrl + O. This will confirm you for the path, press enter and the file is saved. To exit, press Ctrl + X .

Creating a file using bash redirection:

Now, this might be an advanced concept. If you type something like this:

echo '<?php echo "Hello World!"; ?>'

Then, it will output the string ‘<?php echo “Hello World!”; ?>’ on the screen. Now, we are going to use a redirection operator > to redirect the output to a file.

echo '<?php echo "Hello World!"; ?>' > hello.php

Creating an empty file with touch:

Touch is basically a tool to change file timestamps. It updates the timestamp of a file to the current system time. If a file is not found, then it will be created.

touch hello.php

#2.2: Creating a directory:

To create a directory, we simply use the mkdir command.

Create a directory named foo in the present working directory:

mkdir foo

Create a directory foo, inside the bar directory of pwd:

mkdir bar/foo

It says, the bar directory is not present, so,

Recursively create nested directories:

mkdir -p bar/foo

#2.3: Moving/Renaming a file/directory:

Files or directories can be moved using the same command mv. The common synopsis is:

mv path/to/source path/to/destination

But for different situations, it behaves a bit differently (better said, accordingly). For files, it is simple.

Move a file foo.php from pwd to /var/www:

mv foo.php /var/www

Note that in this case, the destination directory www should be present. Otherwise, a new file, named www will be created inside the /var directory.

Renaming a file:

Say, we want to rename the file foo.php to bar.php under pwd:

mv foo.php bar.php

Renaming and moving a file:

Combining the both, we move the file foo.php to /var/www and rename it to bar.php

mv foo.php /var/www/bar.php

Easy? Now let us see the usage of the same function for directories.

Renaming a directory:

Say we have a directory named foo inside pwd. We want to move it to rename it to bar.

mv foo bar

If the directory bar is already present, then foo will be moved inside it. If there is a file present named bar, then it will throw an error. Otherwise, the same will be renamed.

Moving a directory:

Say, we want to move the directory foo to the /var/www. From the last discussion, you should be able to answer it yourself.

mv foo /var/www

Again, if the directory www is not present inside the /var then, the foo will be moved to /var and will be renamed to www. Just keep these things in your mind while working with mv.

#2.4: Copying Files/Directories:

The command cp can copy files and directories to a new location with a new name. Here are some example.

Copy foo.php to /var/www:

cp foo.php /var/www

If www directory is not present, then a file named www will be created inside the /var directory and will have the same content as foo.php. If www is a file, then it will be overwritten.

Copy foo.php and paste in the same directory with name foo.php.bak:

cp foo.php foo.php.bak

Copy foo.php inside /var/www and rename to bar.php on the fly:

cp foo.php /var/www/bar.php

Copy directory bar (with all its contents) to /var/www:

For copying directory, we need to append -r to the command. It tells to recursively copy all the directories and subdirectories and files inside it.

cp -r bar /var/www

Without a -r, the cp command will produce an error saying Omitting directory `bar’

#2.5: Removing Files/Directories:

For deleting files and/or directories we can use rm command. If a directory is empty, then we can also delete using rmdir command.

Deleting a file:

The synopsis is:

rm path/to/file

Deleting a file named foo.php inside pwd:

rm foo.php

Deleting a file named bar.php from /var/www:

rm /var/www/bar.php

Deleting a directory with all it’s contents recursively:

It requires to append a -r to the rm command.

rm -r path/to/dir

Deleting a directory named backup from /var/www:

rm -r /var/www/backup

Deleting an empty directory foo inside pwd:

For safely deleting empty directories, we use rmdir.

rmdir foo

Deleting an empty directory along with empty parent directories:

rmdir -p foo/bar/mydir

The above will delete first the foo/bar/mydir making foo/bar empty. Then in the same way it will delete foo/bar and foo. If any of the directory is not empty, then it will produce an error and will not delete it.

#3: Bulk File/Directory operations:

The above were almost everything about single file/directory operation. But in many cases, you might want to bulk move and/or copy. In such cases, we use filename wildcard. In simpler word, if we want to represent anything for a filename, we use the wildcard *. Similarly, if we want anything followed by bar in the filename, we use *bar. Also, if we want to refer to all .php files, then we use the filename wildcard *.php.

Copy all the files inside the directory bar to /var/www/foo:

cp bar/* /var/www/foo

It will also produce a notice saying Omitting directory `bar/dir’ if any directory is present inside it.

Copy all the files and directories inside the directory bar to /var/www/foo:

cp -r bar/* /var/www

This will also overwrite if anything is present.

Interactive prompt when overwriting files:

cp -ri bar/* /var/www

Copy the directory bar itself to /var/www with all its contents:

cp -r bar /var/www

Delete all the .bak files inside the pwd:

rm *.bak

This will produce a notice if, say a directory named, abc.bak was present. As said earlier, rm, alone, can not remove a directory.

Delete everything inside the pwd:

rm -r *

Delete everything inside /tmp:

rm -r /tmp/*

Delete just the files inside /tmp:

rm /tmp/*

This will also produce notices, saying Omitting directories `…’

Delete everything from the pwd:

rm -r *

Be careful while using this command. This can (and will if permission allows)  recursively wipe clean all files and directories.

#Conclusion:

So, that was all about common and a bit advanced file and directory operations. I have tried to give practical examples by doing the stuff inside directories /var/www, /tmp etc. Please be advised that those directories are not meant for playing with these commands and should probably not execute anything there, unless you know exactly what you are doing. To know more about any of these commands, you always use the man command. For example, if you want to know more about the command cp, then you can type:

man cp

and it will give you everything you need. Also, you can ask here in the comments if you are facing any trouble.

That’ll be all for this. Hope you find it useful and do give your feedback.

External Resources: