How to use 'mv' command to move files except those in a specific directory?
Lets's assume the dir structure is like,
|parent
|--child1
|--child2
|--grandChild1
|--grandChild2
|--grandChild3
|--grandChild4
|--grandChild5
|--grandChild6
And we need to move files so that it would appear like,
|parent
|--child1
| |--grandChild1
| |--grandChild2
| |--grandChild3
| |--grandChild4
| |--grandChild5
| |--grandChild6
|--child2
In this case, you need to exclude two directories child1
and child2
, and move rest of the directories in to child1
directory.
use,
mv !(child1|child2) child1
This will move all of rest of the directories into child1
directory.
How to move files and directories excluding one specific directory to this directory
Execute the following command first in terminal. This extends regexes.
shopt -s extglob
Now you can execute the following mv command
mv !(<file/dir not to be moved>) <Path to dest>
For example, If you are at ~/Test and you need to move all except ~/Test/Dest to ~/Test/Dest, you can execute it as given below, assuming you are at ~/Test
mv !(Dest) ~/Test/Dest
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.
Move all files except some files
With GNU bash 4:
shopt -s extglob
echo mv !(log_20160[34]*.txt) other_folder
If everything looks okay remove echo
.
See: https://stackoverflow.com/a/32309080/3776858
How to move all files in a subfolder except certain files
This is just a suggestion for your change strategy and not about xargs
. You only need the bash
shell and mv
for the external tool.
#!/usr/bin/env bash
shopt -s nullglob extglob
array=(
folder.jpg
log.txt
Temp
Cache
Completed
)
to_skip=$(IFS='|'; printf '%s' "*@(${array[*]})")
for item in /Public/Downloads/*; do
[[ $item == $to_skip ]] && continue
echo mv -v "$item" /Public/Downloads/Completed/ || exit
done
- Remove the
echo
if you think that the output is correct. - Add the
-x
e.g.set -x
(after the shebang) option to see which/what the code is doing orbash -x my_script
, assumingmy_script
is the name of your script.
Move all folders except one
Maybe you are looking for something like this?
The answer to my question there states that what you are trying to to is achievable by using the extglob
bash shell option. You can turn it on by executing shopt -s extglob
or by adding that command to your ~/.bashrc
and relogin. Afterwards you can use the function.
To use your example of moving everything from dir1
except dir1/src
to dir2
, this should work:
mv -vt dir2/ dir1/!(src)
Example output:
$ mkdir -pv dir1/{a,b,c,src} dir2
mkdir: created directory 'dir1'
mkdir: created directory 'dir1/a'
mkdir: created directory 'dir1/b'
mkdir: created directory 'dir1/c'
mkdir: created directory 'dir1/src'
mkdir: created directory 'dir2'
$ ls -l dir1/
total 16
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 a
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 b
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 c
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 src
$ ls -l dir2/
total 0
$ shopt -s extglob
$ mv -vt dir2/ dir1/!(src)
'dir1/a' -> 'dir2/a'
'dir1/b' -> 'dir2/b'
'dir1/c' -> 'dir2/c'
$ ls -l dir1/
total 4
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 src
$ ls -l dir2/
total 12
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 a
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 b
drwxrwxr-x 2 dw dw 4096 Apr 7 13:30 c
More information about extglob can be found here.
How to mv all files under a folder exclude some specify files
Demonstrating an extglob that does work:
mkdir -p "/tmp/$$" && cd "/tmp/$$"
touch {foo,bar}{'(1)',}.csv
shopt -s extglob
printf '%q\n' !(*[()]*).csv
...properly emits:
bar.csv
foo.csv
Because you didn't show the extglob that didn't work, we can't speak to why.
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.
move only specific folders and subfolders using mv
Apparently bedtools
does not exist in your local path. And about the other error message mv: inter-device move failed: ‘/home/cmccabe/Desktop/NGS/API/6-10-2016/bam/coverage’ to ‘/media/cmccabe/My Book Western Digital/coverage’; unable to remove target: Is a directory
, probably that folder is not writable for your user. Move mv
command will remove it from the device but probably you don't have the rights.
Check trying as root (with sudo
) for the second problem. For the bedtools
folder, please check your local path.
Related Topics
How to Get Details of All Modules/Drivers That Were Initialized/Probed During the Linux Kernel Boot
How to Do 'Ret' Instruction from Code at _Start in MACos? Linux
How to Run a Shell Script by Cron Job
Bash Script to Find and Display Oldest File
Squid: How to Disable Authentication
Searching a Process' Memory on Linux
How to Solve "Server Terminated Early with Status 127" When Running Node.Js on Linux
How to Use an Older Version of Gcc in Linux
Command Not Found in Bash's If-Else Condition When Using [! -D "$Dir"]
Linux: Modpost Does Not Build Anything
Is There a Special Restriction on Commands Executed by Cron
Where Does Pp (Par) Unpack Add (-A) Files
New Scala Worksheets Not Evaluated in Eclipse