One of your problems is that you left out the double quotes around the command substitution, so the output from the date
command was split at spaces. See Why does my shell script choke on whitespace or other special characters? This is a valid command:
cp -a /home/bpacheco/Test1 "/home/bpacheco/Test2-$(date +"%m-%d-%y-%r")"
If you want to append to the original file name, you need to have that in a variable.
source=/home/bpacheco/Test1
cp -a -- "$source" "$source-$(date +"%m-%d-%y-%r")"
If you're using bash, you can use brace expansion instead.
cp -a /home/bpacheco/Test1{,"-$(date +"%m-%d-%y-%r")"}
If you want to copy the file to a different directory, and append the timestamp to the original file name, you can do it this way — ${source##*/}
expands to the value of source
without the part up to the last /
(it removes the longest prefix matching the pattern */
):
source=/home/bpacheco/Test1
cp -a -- "$source" "/destination/directory/${source##*/}-$(date +"%m-%d-%y-%r")"
If Test1
is a directory, it's copied recursively, and the files inside the directory keep their name: only the toplevel directory gets a timestamp appended (e.g. Test1/foo
is copied to Test1-05-10-15-07:19:42 PM
). If you want to append a timestamp to all the file names, that's a different problem.
Your choice of timestamp format is a bad idea: it's hard to read for humans and hard to sort. You should use a format that's easier to read and that can be sorted easily, i.e. with parts in decreasing order of importance: year, month, day, hour, minute, second, and with a separation between the date part and the time part.
cp -a /home/bpacheco/Test1 "/home/bpacheco/Test2-$(date +"%Y%m%d-%H%M%S")"
cp -a /home/bpacheco/Test1 "/home/bpacheco/Test2-$(date +"%Y-%m-%dT%H%M%S%:z")"
Yes, you can use find
to look for non-executable files of the right size and then use file
to check for ASCII. Something like:
find . -type f -size 1033c ! -executable -exec file {} + | grep ASCII
The question, however, isn't as simple as it sounds. 'Human readable' is a horribly vague term. Presumably, you mean text. OK, but what kind of text? Latin character ASCII only? Full Unicode? For example, consider these three files:
$ cat file1
abcde
$ cat file2
αβγδε
$ cat file3
abcde
αβγδε
$ cat file4
#!/bin/sh
echo foo
These are all text and human readable. Now, let's see what file
makes of them:
$ file *
file1: ASCII text
file2: UTF-8 Unicode text
file3: UTF-8 Unicode text
file4: POSIX shell script, ASCII text executable
So, the find
command above will only find file1
(for the sake of this example, let's imagine those files had 1033 characters). You could expand the find
to look for the string text
:
find . -type f -size 1033c ! -executable -exec file {} + | grep -w text
With the -w
, grep
will only print lines where text
is found as a stand-alone word. That should be pretty close to what you want, but I can't guarantee that there is no other file type whose description might also include the string text
.
Best Answer
You can try something like:
in a script, if your
date
supports--reference
, which picks up the last modification date of the reference file.