Shell perl scripts one liners

An equivalent of the other find-replace, except it's a one-liner that generates no temp files, and is more flexible:

perl -pi -e 's/find/replace/g' *.txt

Or, to change matching files in a hierarchy:

find . -name '*.txt' |xargs perl -pi -e 's/find/replace/g'

Find the full name of a user
If you want to find out the full name for a user name you can use one of these one-liners to do the job:

ypmatch matkin passwd | cut -d: -f5 | cut -d, -f1
grep "^matkin:" /etc/passwd | cut -d: -f5 | cut -d, -f1


Remove processes matching some regular expression
If you have a number of processes that you want to kill, one of the following one-liners might be useful:

kill `ps xww | grep "sleep" | cut -c1-5` 2>/dev/null
ps xww | grep "sleep" | cut -c1-5 | xargs kill 2>/dev/null

This will kill any processes that has the word "sleep" in the calling command. If your kill does not handle multiple pids' you can either use the one-liner

ps xww | grep "sleep" | cut -c1-5 | xargs -i kill {} 2>/dev/null
or use a for-loop:
for x in `ps xww | grep "sleep" | cut -c1-5`
do
kill $x 2>/dev/null


To list all files in the `/usr/local' directory tree that are greater than 10,000 kilobytes in size, type:

$ find /usr/local -size +10000k [RET]

To list all files in your home directory tree less than 300 bytes in size, type:

$ find ~ -size -300b [RET]

To list all files on the system whose size is exactly 42 512-byte blocks, type:
$ find / -size 42 [RET]



Use the `-empty' option to find empty files -- files whose size is 0 bytes. This is useful for finding files that you might not need, and can remove.
To find all empty files in your home directory tree, type:
$ find ~ -empty [RET]


No comments:

Other Articles

Enter your email address: