Linux Tips: File & Directory Handling

driveMy desktop and all of the servers I own are Linux or Unix based. So as a result I tend to find myself in a terminal navigating file systems and working with files more often than not.
Command line file management allows you to wield crazy amounts of power but it doesn’t have to be all hard work. Here I am going to run you through some of my favorite simple file and directory related tips for working at the command line.

Directory Navigation

When in a directory you can navigate to the last directory you were in with the command:

cd –

The location of your last directory location is also stored in the $OLDPWD variable. This is pretty handy and can be used in conjunction with other commands. For example to delete the directory you were last in and all its contents you would use:

rm -rf $OLDPWD

The cd dash trick is handy when just switching between two directories but the method tends to come unstuck if you need to add another directory to the mix. In this situation its easier to use the pushd and popd commands. pushd allows you to push the current working directory on a stack for later retrieval. The popd then allows you to switch back to a path stored on the stack earlier with the pushed command i.e

[codesyntax lang=”bash”]

pwd
/etc/apache2
pushd .
cd /usr/local
cd /etc
popd
pwd
/etc/apache2

[/codesyntax]

If you find the stack is too limiting for your needs you can also assign paths to BASH variables to help simplify navigation around the file system. i.e

[codesyntax lang=”bash”]

DIR1=’/etc/apache2/sites-enabled/’
DIR2=’/var/log/apache2/’
cd $DIR1
pwd
/etc/apache2/sites-enabled/
cd $DIR2
pwd
/var/log/apache2/

[/codesyntax]

Assigning BASH variables when performing complex operations also has the added benefit of allowing you to couple the locations with commands ie to output the contents of the mail log with cat

LOGDIR=’/var/log/’
cat $LOGDIR/mail.log

File Handling

Removing Files

Sometimes you will come across a directory that contains a huge number of files executing rm *, results in an ” Argument list too long” error. To resolve this files need to be fed into remove in batches that the command can handle, one of the quickest ways to achieve this is is by using the find command to feed the rm program with input:

find * -xdev -exec rm -f ‘{}’ ‘;’

Emptying Files

Sometimes its handy to quickly wipe the contents of file while not deleting the file altogether, for example logfiles. Most people simply use rm to remove the file and then create a new file using the touch command with the same name. The same thing can be achieved alot easier by simply using the > operator eg

>mylogfile.log

Will completely clear the contents of the file mylogfile.log if a file by that name actually exists. If it doesnt an empty file with that name will be created for you.

Comma Operator

The comma operator can make your life alot easier when performing multiple file operations. For example to create two empty files with different extensions:

touch newfile.{txt,xml}

The extension for the xml file can then easily be changed to html by combining the comma operator with the mv command i.e

mv newfile.{xml,html}

Delete With Exceptions

Some times you would like to delete all of the files within a directory except a certain type of file. For example you have a directory full of miscellanous images and you would like to delete all of them except those in JPEG and PNG format.

rm !(*.jpg|*.png)

This will delete all of the images in the directory that do not possess a .jpg or .png.

NOTE: Of course the command above would still delete jpeg files with the extension .JPG so some care must still be taken.

Further Reading:

The GNU Bash Reference Guide