Reputation: 23
I'm attempting to copy a series of files and folders using BASH in Ubuntu. It is important for the files to be copied in alphabetical order because they are going in a digital picture frame that reads the files in the order in which they were added to the flash drive. From what I've discovered, Nautilus copies them based on the inode number, so they end up out of order on the picture frame.
From the searching I've done, I've come up with the following command:
for i in "$(find * -type f -print0 | sort)"; do cp -v "$i" "/media/PicFrame/$i"; done
which results in an error that the file name is too long. It appears that BASH is trying to make a file with every file name separated by "\n". I've tried removing some of the quotes, but that results in several "cannot stat" errors when trying to copy pieces of files names (since many have spaces in them).
Any ideas on how to make this work? I'm open to other methods, but I'd really like this to be a single command/script that I can run each month when I change out the pictures. (I have been doing this in Windows, but am trying to do everything in Ubuntu now. I've been back and forth for about two years now.)
Upvotes: 2
Views: 4119
Reputation: 360015
As Ignacio has mentioned, you should use a while read
loop if you're using find
.
If you don't need recursion (your files are all in one directory level), you can use a for
loop with globbing. When using this technique, you don't need to use sort
since the glob is guaranteed to be expanded in lexicographical order for your locale (in other words "sorted").
for file in *
do
cp "$file" dest
done
You should note that part of the problem with the script in your question is that you're using print0
to pipe the output of find
into sort
. In order to have that work, you will need to the appropriate option to sort
if your version has it.
find ... -print0 | sort --files0-from=-
Upvotes: 2
Reputation: 798606
Use read
in a while
loop instead.
find ... | while read file
do
cp ..."$file"...
done
You may want to consider using rsync
instead though.
Upvotes: 3