Shammir
Shammir

Reputation: 989

How to solve no such file or directory while using xargs

I am trying to copy a file(s) to the same directory but with a prefix. I am using xargs to do that. NOTE: I can't use cd as it will break build in my pipeline. This is what I have

root@gotoadmins-OU:~# ls
snap  test

root@gotoadmins-OU:~# ls test/
me.war

root@gotoadmins-OU:~# ls test | xargs -I {} cp {} latest-{} test/
cp: cannot stat 'me.war': No such file or directory
cp: cannot stat 'latest-me.war': No such file or directory

Upvotes: 0

Views: 3052

Answers (2)

izzy18
izzy18

Reputation: 804

If I understand the question correctly, you simply want to copy all of the files in a subdirectory to the same subdirectory, but with the prefix "latest-".

find test -type f -execdir bash -c 'cp "$1" latest-"$(basename "$1")"' "$(which bash)" "{}" \;

$(which bash) can be replaced with the word bash or anything, really. It's just a placeholder.

As @KamilCuk commented, a subshell might also be an option. If you put commands inside parentheses (e.g. (cd test; for FILE in *; do cp "$FILE" latest-"$FILE"; done)), those commands are run in a subshell, so the environment, including your working directory, is unaffected.

Upvotes: 2

Tommy Bailey
Tommy Bailey

Reputation: 123

can you just use the cp command to do this?

cp test/me.war test/latest-me.war

Upvotes: 1

Related Questions