I am trying to copy a file (let's call it list.txt) that is in several places and give it a new name. Some folders should be ignored (e.g. those containing _old)
Assume the following structure:
/data/folder1/subfolder/user/list.txt
/data/folder2/subfolder/user/list.txt
/data/folder2/subfolder_old/user/list.txt
/data/folder3/anothername/other/list.txt
/data/folder3/anothername/ignorethisfolder/list.txt
With following command I get the exact files listed that I expect:
ll /data/*/{subfolder,anothername}/{user,other}/*.txt 2>/dev/null
That's what I want to use for copying and the following:
cp /data/*/{subfolder,anothername}/{user,other}/*.txt /data/*/{subfolder, anothername}/{user,other}/*.txt_backup 2>/dev/null
Unfortunately, this does not deliver the desired result. What should the command for the copying process be?
CodePudding user response:
Unfortunately the cp
command doesn't work that way, you need to copy each file independently:
for f in /data/*/{subfolder,anothername}/{user,other}/*.txt
do
cp "$f" "${f}_backup"
done
CodePudding user response:
You could use the find command:
find /data -not -path "*/*_old/*" -name "list.txt" -type f -exec cp {} {}_backup \;
Explanation
find /data
traverse your /data directory-not -path "*/*_old/*"
exclude any paths with a directory ending in _old-name "list.txt"
select files/directories with the name list.txt-type f
only select files-exec cp {} {}_backup \;
execute cp on all remaining matching paths the {} is replaced with the match path and the ; is needed to end the exec statement
note
You can add your 2>/dev/null
if you still get errors on some results.