I have two directories. DirA
contains all the files which are in DirB
. I want to remove all the files which are in DirB
from DirA
. How one could do it in linux command line?
I am using ubuntu.
Thanks
cd DirB
for i in *
do
rm DirA/"$i"
done
Edit: Use double-quotes around $i
to handle filenames containing spaces.
You can do that i for loop:
for f i DirB/*; do
fn="${f##*/}"
[[ -f "$fn" ]] && rm -f "DirA/$fn"
done
Here you have an example, with execution output, which would work also if filenames would contain spaces (an uncomfortable thing which I don't recommend, by the way):
root@folgore:/tmp/test# tree
.
├── DirA
│ ├── a
│ ├── b
│ ├── c
│ └── d
└── DirB
├── a
├── b
├── e
├── g
├── h
└── r
2 directories, 10 files
root@folgore:/tmp/test# for f in `ls DirB/* | sed 's/ /_SPC_/g'`;do fa=`echo $f | sed 's/_SPC_/ /g;s/^DirB\//DirA\//'`;echo "removing $fa if exists";rm -f "$fa";done
removing DirA/a if exists
removing DirA/b if exists
removing DirA/e if exists
removing DirA/g if exists
removing DirA/h if exists
removing DirA/r if exists
root@folgore:/tmp/test# tree
.
├── DirA
│ ├── c
│ └── d
└── DirB
├── a
├── b
├── e
├── g
├── h
└── r
2 directories, 8 files
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.