leo charre


bash grab bag

Some posix utilities can be a mindfuck. They can also be incredibly useful.

Do something for all files in a directory

I have a directory with txt files. I want to move all files which filename is a number to another directory.
$ for var in $(ls ./ | grep "^[0-9]\+\.txt$"); do mv $var ./; done;

Parsing arguments delimited by newline

Imageine you have a text file, in which each line is an argument for another program.
An example of this is a list of names, each line is a first and last name. Output of gnu find is the same, when you seek files, they are printed one perl line.

People sometimes ask how do you interpret results of ‘find’ as arguments, especially when they are newline delimited.

For example, I recently had to get md5 sums for a ton of pdf files.
This turns out to be an expensive operation, so I did it in steps.

  1. I save where the files are to a text file.
    find ~/ -type f -iname "*.pdf" >> /tmp/found_pdfs
  2. Next.. I need to feed the paths as arguments to md5sum..
    xargs --delimiter=\\n --arg-file=/tmp/found_pdfs md5sum >> /tmp/found_pdfs_md5sums

Now, this oculd have been done with find’s -exec parameter, but I’m concerned this would take longer..
find ~/ -type f -iname "*.pdf" --exec md5sum '{}' >> /tmp/fond_pdfs_md5sums

See the beginning or end of a text file

You have a large text file and you need to see the end or beggining..
Beggining..
head ~/file.txt
End..
tail ~/file.txt

But but.. I need to see more! Maybe 2000 lines?
tail -n 2000 ~/file.txt

What if you are looking at a log file that is getting appended at all times? For example, wouldn’t you like to see the log file to your http server.. as a cgi prints to stdout (warnings, etc)..

tail -f /var/log/http/error_log

You’ll notice after that command, the screen appears to hang- no.. It’s just waiting for something else to generate an error and you’ll see it real time! Very cool.

Strange hardware errors

If you have weird startup errors, or hardware issues.. For example you are trying to plug in a digital camera via usb- and something is funny. First off.. try the command ‘dmesg’ for valuable information.

Leave a Reply


Linux User