Thursday, February 01, 2007

Some command line shortcuts

I know it's been a while, and this is only a short one, but I wanted to get these down.

I've been experimenting with the command line strengths of Linux, combining commands to perform powerful statements. The first is:

find -name "*.JPG" -exec rename 's/\.JPG$/\.jpg/' {} \;

This command will find every file within a tree structure from the point the command is run that has .JPG as it's extension and then rename it to .jpg. Linux automatically recognises .jpg to be a picture but not .JPG. For someone like me with over 3000 pictures in numerous subdirectories this was a huge productivity help. It is simple to modify it for other file types too. Obvious ones spring to mind like .AVI, .BMP, .MPG etc.

Another thing I wanted to do was increase the rights on a tree structure for all files whilst removing execute. I know from painful past experience that chmod -R 0664 * has disastrous results with directories! However, combining find and chmod allowed me to produce these 2 commands:

find -type f -exec chmod 0664 {} \;
find -type d -exec chmod 2775 {} \;

The first only finds files and then changes the permissions to give the user and group read and write, allow everyone read, and removes execute across the board.

The second finds directories and changes the permissions as above (whilst allowing execute because they are directories), and also changes the permissions on the directory to ensure that every new file is given the group ownership of the directory, and not the user creating it.

As I continue to play with regular expressions and the command line I'm sure I'll find more of these gems. I'll probably put them here too.

For now, one more:

find -name "*.zip" -exec unzip -o {} \;

OK, one more:

rename 's/^\d+_//' *

This will remove the leading number and underscore from files or directories such as those that have been downloaded using .nzb files from newsgroups. Useful for tidying up your downloaded directory structure. (Or at least useful for tidying up mine!)

No comments:

Top Tracks of 2012

Well, it's that time of year. Once again I can abuse my html knowledge and shove a few YouTube videos into a blog post to illustrate wha...