Home > Blockchain >  Moving files from one directory to other by skipping one directory
Moving files from one directory to other by skipping one directory

Time:12-21

I want to move all files from SrcFiles to TgtFiles but I should not copy the files from SrcFiles_Backup to TgtFiles using bash- shell script.

Source Path - /test/dev_env/proj/Data/ALB/ALBT/SrcFiles/SrcFiles_Backup

Target Path - /test/dev_env/proj/Data/ALB/ALBT/TgtFiles/

mv /test/dev_env/proj/Data/ALB/ALBT/SrcFiles/* /test/dev_env/proj/Data/ALB/ALBT/TgtFiles/

The above command is copy all files and folders inside SrcFiles. I want to copy all files from SrcFiles Folder. Can anyone guide me on this?

Thanks!

CodePudding user response:

You can use a for loop to loop over all of the files in SrcFiles and then use grep -v to ignore the SrcFiles_Backup sub folder and only move the other files.

For example,

src_file_dir=/test/dev_env/proj/Data/ALB/ALBT/SrcFiles
tgt_file_dir=/test/dev_env/proj/Data/ALB/ALBT/TgtFiles

for file in $(ls $src_file_dir | grep -v "SrcFiles_Backup"); do
    mv $src_file_dir/$file $tgt_file_dir/$file
done

Does that meet your needs?

  • Related