Add Me!Close Menu Navigation

My technical corner about Linux, Perl, programming, computer networks and network security

Add Me!Open Categories Menu

Useful one-liners, part I

Find

1) Find the all files in the working directory (and subdirectories) if these have “*.doc” extension and execute some command. To do this, I use simple a regular expression and the echo command:

find . -type f -iname "*.[dD][oO][cC]" -exec echo {} \;

 

2) Delete all files and directories in the working directory except somedir:

find . \( ! -regex '^\.\/somedir.*' \) -exec rm -rf {} \;

 

3) Find the all files in the working directory (and subdirectories) if these have the 1MB size and if these are older than 7 days. Do something (echo in this case) command on the files.

find . -type f -ctime +7 -size +1M -exec echo {} \;

 

Perl

Change AAA to ZZZ on all files in the working directory

perl -i -pe 's/AAA/ZZZ/igs' *.*

 

Bash

Do some command on all files with specified type in the working directory (*.zip in this case)

for i in *.zip; do unzip $i done

Leave a Reply

You must be logged in to post a comment.