This page was last modified: July 24 2008 00:54:01   
Too Cool for Internet Explorer

Files and directories

This page is intended for various tips 'n tricks for managing files and directories.

rm returns 'too many arguments'

It came to a point where my mail inbox contained over 17000 mails from the system. I tried to delete them with rm *, but it returned 'too many arguments'

I searched the web, and found this solution

find . | xargs rm -f

The above will delete all files in the current directory. But you can also specify a pattern for matching specifik filenames:

find . | "filename pattern" | xargs rm -f

Delete files older than...

This one will remove all files older than 30 days, in a specific directory.:

find /path/to/directory -type f -mtime +30 -delete

Use this one to make a test-run. It does not delete anything, but prints the names of the files that meet the 30-day criteria:

find /path/to/directory -type f -mtime +30 -exec ls -l {} \;

The -mtime +30 part of the command, says 'modified more than 30 days ago'. You can change it to 'accessed more than 30 days ago' by using -atime +30 instead. In many cases atime is more secure, since you won't delete files which a rarely modified but frequently accessed.

Here's a tip for finding directories consuming the most diskspace:

cd /home && du -sk * | sort -rn | head

The above command will show you a list of the top 10 directories in /home, using the most diskspace.

Using a command on multiple files and/or directories

You can run any command on multiple files with find. Here is an example:

find . -type f | xargs chown joe:www

The above will change owner to joe and group to www on all files in the current directory and any subdirectories. The -type option is set to f, which means that only files will be affected.

find . -type d | xargs chmod 755

This will change permissions to 755 on all directories and subdirectories, in the current directory.

Get total size of folder

The 'du' command will tell you the size of a folder including files and subfolders.:

du -ksh foldername/

Put a tail on the file

You can follow a file with the tail command. This command will display the last lines of a textfile which is very useful if you want to see what happens in a file. For example let's say you are debugging your mailsystem, and want to se what happens while a mail is received:

tail -f /var/log/maillog

The f option makes tail wait for additional data to be appended. By default the last 10 lines is shown. You can change that with the n option. The example below shows how to tail the last 20 lines of the file:

tail -f -n 20 /var/log/maillog

Use [Ctrl]+C to stop tailing...

Decompress (unpack) a bz2 file

bzip2 -d filename.bz2

How to create a compressed archive

Create a compressed archive called archive.tar containing file1.txt, file2.txt and file3.txt:

tar -czf archive.tar.gz file1.txt file2.txt file3.txt

Create a compressed archive called archive.tar containing the directory mydir and everything in it:

tar -czf archive.tar.gz mydir

Create a compressed archive called archive.tar containing all files beginning with dog:

tar -czf archive.tar.gz dog*

How to decompress and unpack a compressed archive

Decompress and unpack the files and/or directories into the current directory:

tar -xzf archive.tar.gz

Note that the archive does not dissapear, you'll have both the archive and the unpacked files. Just delete the archive if you don't need it anymore.

How to make a soft link

I always forget the order of the options when creating a soft link, so here goes:

ln -s <path to destination> <link name>

Example:

ln -s /usr/local/etc link_to_etc