[Home]LinuxHints/UsefulOneLiners

HomePage | LinuxHints | RecentChanges | Preferences

From ThomasAdam:

Rename all files in a directory to lower case

mmv "[A-Z]" "[a-z]" * 

Or, for Debian users, assuming all files are in the same directory:

rename 'y/A-Z/a-z/' *
and assuming that they are in subdirectories under the current directory:
find -type f | xargs rename 'y/A-Z/a-z/'
(Note that the Debian version of "rename" is not the same as the version shipped with many other distributions. The above won't work on Red Hat, for example. It is perl based.)

From ThomasAdam:

This example also is potentially dangerous, as it assumes *all* files in every subdirectory below the one you invoke it from is needed. To just change just those files in the current working directory:

find . -type -f -maxdepth 1 -exec rename 'y/A-Z/a-z/' {} \;

or to be consistent with Hugo's original style:

find -type -f -maxdepth 1 | xargs rename 'y/A-Z/a-z/'

There is no difference between the two commands other than xargs can handle a slightly larger command-line chain.

Of course, what happens when a file contains a space? Or indeed, a directory? In instances such as this, you *could* do:

for i in *; do rename "$i" 's/some/expression/'; done

But this is cumbersome, since it invokes a new 'rename' process each time the loop is iterated. With find though, one can do:

find . type -f -maxdepth 1 -print0 | xargs -0 rename 'y/A-Z/a-z/'

The -print0 option to find, means treat the string, not delimiting on "\n" (the normal) but null-terminated strings ('\0') hence this encapsulates spaces. The -0 option to xargs is used to tell it that it is to delimit on null-terminated strings.

From: DanPope

There is also a perl script called chcase available at http://www.blemished.net/chcase.html which can do pretty much all of these things even more simply:

chcase *                    # change all files in current directory to lowercase
chcase -u *                 # change all files in current directory to uppercase
chcase -r *                 # also rename directories in the current directory
chcase -r '*.JPG'           # change all jpegs to lowercase recursively
chcase -x 's/foo/bar/' *    # run a regular expression on all files

Renaming upper-case directories to lowercase

From: ThomasAdam:

Following on from the example above, often it is the case that the directories are also in uppercase. Ugh. This is horrid, and not at all Unix-like. The following will take care of that:

cd /somewhere 
find . -type d -depth -name '*[A-Z]*' -print |
  while read dir; do dirn="$(dirname $dir)"; basen="$(basename $dir)";
  newbasen="$(echo $basen | tr [:upper:] [:lower:])"; mv "$dir" "$dirn/$newbasen"; done

Use at your own risk, as it is imperfect. It will fail if the directory has spaces or \n characters in them. For the inquisitive, you might be wondering why I am piping the output to the shell. Quite simply is because the '-exec ' option to find (which I would have used) is not passed to the shell, which is what we need to use here to accomplish this task.

Tracking down large files

From: ThomasAdam:

You may find that your system is full, in which case:

cd /directory && du -sk *

Here's another way:

find /some_directory -size +5000k -ls 

Which will list files over 5000kb (5MB). Change as appropriate.

Clock Skew during make?

From: ThomasAdam:

Sometimes, when you are compiling an application from source (this may be more applicable to gentoo users) you may well get an error message like:

Make ** Error [1] Clock Skew... **

This means that the files you are using have a timestamp that is in the future, relative to the time and date that your system clock is set to. This can be avoided at the untarring stage, by specifying the "m" flag to tar, for instance:

$ tar xzvfm ./some_file.tar.gz

which will preserve the modification times of the files untarred (often tar will report the "modification time set in the future" which is an indication that you should use the "m" flag). Often though one does not need to do that, and so the following command will get things back on track:

find . -type f | xargs touch -c && make 

Finding files containing a string in a directory hierarchy

From ThomasAdam:

In this example, all .php files are being checked for the string mysql:

find . -name '*.php' -type f | xargs grep -H 'mysql'

From AndyRansom:

In this example, line numbers are returned (using -n) and the search is case-insensitive (using -i):

find . -name '*.php' -type f | xargs grep -n -i 'MySQL'

Bulk image resize

From DavidRamsden:

In this example, all jpg files in the current directory only will be resized to 800x600 and placed in a directory called resized:

find . -maxdepth 1 -name '*.jpg' -type f -exec convert -resize 800x600 {} resized/{} \;

In this example, all jpg files in the directory and sub-directories will be resized to 800x600 and placed in a directory called ../resized:

find . -follow -name '*.jpg' -type f -exec convert -resize 800x600 {} ../resized/{} \;

Very useful for when you return from a day trip with your digital camera full of large high-quality images!

convert is a program that's part of the ImageMagick suite.

Testing Samba Share Access

From StephenDavies:

Having problems with access to shares on your network. The testparm command can do more than just list your shares.

testparm /etc/samba/smb.conf www.myserver.com 192.168.0.212
This will test access to your share from "www.myserver.com" who has the address of 192.168.0.212. Especially useful if you are running a firewall.

Alternative to the find command

From StephenDavies:

Instead of running find from / there is an easier way. (This may differ according to your distro) So, for RedHat, just after the install, do the following

cd /etc/cron.daily; ./slocate.cron &

Then the locate command works

# locate <string> | grep <substring>  
Its fast and easy. Much less demanding on the system than find. Then ,
crontab -e
and add
 
59 23 * * * /etc/cron.daily/slocate.cron 
There are other good scripts such as logrotate, etc. also in the /etc/cron* directories

HomePage | LinuxHints | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited February 17, 2005 12:33 am by ThomasAdam (diff)
Search: