I have to write a shell script so that it should remove all the files except test.txt When I run the script, it says Removal of Source file failed because dev_data is a directory.
ls | grep -v 'test.txt' | xargs rm
I want to write a shell script so that if there is a directory in the below path, it should skip the directory. The script should remove only the files inside the directory - dev_data
Error Log is given below:
ig_dir.sh process starts at:
Wed Nov 2 08:08:09 IST 2022
tar: Removing leading `/' from member names
/alb/albt_shard/alb1/path1/path2/alb1/sub/alby/
/alb/albt_shard/alb1/path1/path2/alb1/sub/alby/dev_data/
/alb/albt_shard/alb1/path1/path2/alb1/sub/alby/dev_data/list_file.place.txt
rm: cannot remove ‘dev_data’: Is a directory
ig_dir.sh: Removal of Source file failed
This is how I'm passing the parameter -- sh -x ig_dir.sh.sh /alb/albt_shard/alb1/path1/path2/alb1/sub/alby
.. So when I pass this as argument to the script, it checks for the directory under alby and gives --- rm: cannot remove ‘dev_data’: Is a directory
ls
is itself a bad idea; see ParsingLs. With theextglob
shell option enabled in bash,rm !(test.txt)
is a means to remove all files excepttest.txt
from the current directory; for something that works in non-bash shells, or bash where configuration isn't know, one can usefind . -maxdepth 1 -name 'test.txt' -prune -o -delete
test.txt
from every directory nameddev_data
? Please be explicit about what the goal is here.shopt -s extglob
set in bash, that might befind ./**/dev_data -name test.txt -prune -o -delete
, but that's only if my guess at what you were trying to ask for is correct)tar
error message feature in the pipeline that doesn't mention tar?