Tuesday, August 29, 2017

Find files in Linux and move files

Following command can be used to find files in a directory and move found files into /home/sujith/files/ location.

find . -name "*" -maxdepth 1 -exec mv -t /home/sujith/files {} +

Monday, August 28, 2017

Copy large number of files between Linux servers

You can use "rsync" command to sync data between two locations.

rsync -r dir1/ dir2
sync the contents of dir1 to dir2 on the same system (-r option for recursive)

rsync -a dir1 username@remote_host:destination_directory
sync directory from the local system to a remote system ( pushing files to destination)

rsync -a username@remote_host:/home/dir1 place_to_sync_on_local_machine
sync a remote directory to the local system (pulling files from remote machine)

Also, you can compressed and sync files between locations using following command.

rsync -az source destination

https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps

Thursday, August 24, 2017

Linux shell script to find files and copy into another location

You can use following shell script to find files in directories and copy into another location.

#!/bin/bash
for k in `cat errlog_tmp.txt`; 
do 
arr=( $(find . -name "$k*" -type f));
echo $arr;( cp $arr "./error1$k"); 
done;

Above shell will cat file names in "errlog.txt" and find in the current directories. After successful file found, it is copied into "./error/" directory.