Friday, June 18, 2010

Ubuntu - Copy Everything Except

If you want to copy everything within a directory, except certain files or directories, you can use the following command.
cp -a `ls | grep -v yourexcludepattern` ../destinationdir/

Assume, you want to copy all files, except *.java within from the current directory to the foo directory. Then, the command would be,
cp -a `ls | grep -v *.java` /home/wageesha/foo

Friday, June 11, 2010

How to copy a file into multiple directories in Ubuntu

Assume that you want to copy a file into multiple directories in the current directory, then all you have to do is, just entering following command in the terminal. Here, file.txt will be copied into all directories in the current directory. In case of a directory coping, replace file.txt with -rf [your dir].
for dir in *; do [ -d "$dir" ] && cp file.txt "$dir" ; done
for dir in *; do [ -d "$dir" ] && cp -rf /home/wageesha/images "$dir" ; done