BASH copy all files except one
Simple, if src/
only contains files:
find src/ ! -name Default.png -exec cp -t dest/ {} +
If src/
has sub-directories, this omits them, but does copy files inside of them:
find src/ -type f ! -name Default.png -exec cp -t dest/ {} +
If src/
has sub-directories, this does not recurse into them:
find src/ -type f -maxdepth 1 ! -name Default.png -exec cp -t dest/ {} +
How to use 'cp' command to exclude a specific directory?
rsync
is fast and easy:
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude
You can use --exclude
multiples times.
rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude
Note that the dir thefoldertoexclude
after --exclude
option is relative to the sourcefolder
, i.e., sourcefolder/thefoldertoexclude
.
Also you can add -n
for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n
from command line.
Bash copy files recursively exclude list of some files
This is not the best way to do it, but if rsync is not working, you can use find to composite a list and then remove files from that list.
find . -name "*" | grep -v "<filename you dont want>" | xargs -i cp --parents {} /output/dir/name
The only thing to keep in mind here is that your current directory must be the base directory of the files you want to copy, in order to preserve the parent structure.
note:
Add another | grep -v "<filename you dont want>"
to remove a second file. Alternatively, you can use wildcard matching in one grep grep -v "<file1>\|<file2>"
Copy whole directory but exclude all folders and subfolders with certain name
Instead of cp
, you can use tar
with option --exclude
to control what you want copied or not.
The full command is:
tar --exclude="outdir" -cvpf - . | (cd TARGET_DIRECTORY; tar -xpf -)
- So any path that contains the "outdir" pattern will be excluded.
- Without the
--exclude
option, it will copy the entire structure of your current directory under TARGET_DIRECTORY. - You can replace the
.
in the first tar by your desired source directory.
LINUX CP: I need to copy all files from one directory to another without copying any sub-directories
Glob patterns (the wildcard *.*
) don't work within quotes, but you can move the glob outside the quotes like so:
cp '/media/lm1/My Passport/OneDrive/Music/Yes/'*.* '/home/lm1/Music/Archive'
Copy all directories except one
Use extended globbing:
shopt -s extglob
cp -r dirToCopy/!(dirNotToCopy) .
How to copy all files and directories except one from local drive to HDFS?
--copyFromLocal
does not give a way to exclude particular files. What you can do is the following:
ls /home/my_directory | grep -v 'doNotCopy.txt' | while read -r fileName ; do
eval "hdfs dfs -copyFromLocal -f $fileName /path/to/HDFS/"
done
Now if you ls
your /path/to/HDFS/
you should get the desired files in there:
hdfs dfs -ls -C /path/to/HDFS/
anotherTest.txt my_subdirectory/ subdirectory2/ test.txt
If you want to exclude multiple files or directories
ls /home/my_directory | grep -v 'doNotCopy.txt\|dirDoNotCopy\|anotherTextFile.txt' | while read -r fileName ; do
eval "hdfs dfs -copyFromLocal -f $fileName /path/to/HDFS/"
done
Move all files except one
Put the following to your .bashrc
shopt -s extglob
It extends regexes.
You can then move all files except one by
mv !(fileOne) ~/path/newFolder
Exceptions in relation to other commands
Note that, in copying directories, the forward-flash cannot be used in the name as noticed in the thread Why extglob except breaking except condition?:
cp -r !(Backups.backupdb) /home/masi/Documents/
so Backups.backupdb/
is wrong here before the negation and I would not use it neither in moving directories because of the risk of using wrongly then globs with other commands and possible other exceptions.
Related Topics
Signing Windows Application on Linux-Based Distros
Prevent Linux Thread from Being Interrupted by Scheduler
How to Get the Start Time of a Long-Running Linux Process
How to Compile/Install Node.Js(Could Not Configure a Cxx Compiler!) (Ubuntu)
What Is Path on a MAC (Unix) System
Syntax Error Near Unexpected Token 'Then'
How to Download a File from Server Using Ssh
Setting the Vim Background Colors
Sed with Literal String--Not Input File
How to Set Rpath and Runpath with Gcc/Ld
Count Number of Files Within a Directory in Linux
How to Convert \Uxxxx Unicode to Utf-8 Using Console Tools in *Nix
Write a Bash Shell Script That Consumes a Constant Amount of Ram for a User Defined Time
Using Ls to List Directories and Their Total Sizes
How to Delete All Lines in a File Starting from After a Matching Line