Skip to content

Instantly share code, notes, and snippets.

@dedy-purwanto
Created July 5, 2012 04:42
Show Gist options
  • Save dedy-purwanto/3051434 to your computer and use it in GitHub Desktop.
Save dedy-purwanto/3051434 to your computer and use it in GitHub Desktop.
Bash: Copy files with list of excluded files.
Say you need to copy files in a folder to somewhere else, except that you have a list of files that might be exist in the source folder but you don't want them to be copied. Your text file that has this list is named list.txt:
------------------------
exclude1.mp3
dont_copy.mpeg
nyancat.jpg
------------------------
$ cd source/
$ cat /path/to/list.txt | tr '\n' '#' | sed 's/.$//g' | sed 's/#/\\\\|/g' | xargs -i sh -c "ls | grep -v '{}'" | xargs -i cp -v -u {} /path/to/dest/
This will copy all files in the source/ except those files in the list.txt, if the file is already exist in the /past/to/dest/, it will be ignored (-u).
It's simply printing the list.txt, changing all line breaks to #, and remove the trailing #, and then change # to a regex of OR (\|), and do `ls` and inverse grep with the list of excluded files, and copy the non-excluded files to /path/to/dest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment