Question

Under Linux the following line creates a text file:

find "/path/to/pdfs/" -type f -name "$1*.pdf" -printf "%TY/%Tm/%Td %TH:%TM %p\n" \
    | sort -n -k1.1,1.2 -k1.3,1.4 -k1.6,1.7 -k1.9,1.10 -k2.1,2.2 -k2.4,2.5 -k3 \
    > remoteuser@remoteserver:/u/tmp/CustTmp/zzz_pdfs.txt

This text file contains this:

2013/12/31 16:43 /path/to/pdfs/list1.pdf
2013/12/31 17:36 /path/to/pdfs/list2.pdf
2013/12/31 17:38 /path/to/pdfs/list3.pdf

Under AIX the above command fails because -printf doesn't exist in the find command on AIX. How to work around this limitation?

No correct solution

OTHER TIPS

The problem you're having isn't a shell issue. It's that your find commands aren't compatible.

I am guessing that you are running Bash on a Linux box, and Kornshell on Solaris. The problem is that your Linux box has the GNU version of the find command which has things like the -printf parameter while the find command on your other system does not because it's using the BSD or AT&T Unix version of the command.

What you need to do is to use your system's stat command to pull the information you want from the file:

find "/path/to/pdfs/" -type f -name "$1*.pdf" -exec stat ??? {}\; | sort ...

The problem is I don't know the format of the stat command. Unfortunately, this command varies from system to system. It looks like you're creating a sorting modification date according to the GNU documentation on the find time directives. Here's an equivalent on my Mac running Mavericks:

find "/path/to/pdfs/" -type f -name "$1*.pdf" -exec stat -f "%10Dm %SN" {}\; | sort ...

The -f parameter specifies a format on how stat will print its information. The "%10Dmspecifies 10 decimal digits (D) of the modification timem. The%SN` specifies the file's name in string format. This prints the date as the number of seconds from the Epoch (usually Jan 1, 1970).

In your original sort, you were specify each and every field of the date time stamp (which wasn't really necessary, so your sort will be simpler:

find "/path/to/pdfs/" -type f -name "$1*.pdf" -exec stat -f "%10Dm %SN" {}\; \
    | sort -k1,1n -k2,2

Unfortunately, that doesn't print out the time in a nice neat format like you want. So, we'll strip off the file name after the sort and pass that through stat again printing the file

find "/path/to/pdfs/" -type f -name "$1*.pdf" -exec stat -f "%10Dm %SN" {}\; \
    | sort -k1,1n -k2,2  sed 's/.* //'
    | while read file
    do
        stat "%-22.22Sm %N" $file
    done

This will print out your files as:

Dec 12 15:38:43 2013   /path/to/pdfs/list1.pdf

Not exactly the same, but it's close. Actually, on my Mac, I can take the numeric output of the date from the first stat command, and convert it into the date string in the exact format you want.

Unfortunately, this particular feature of the date command has the same problem as the -printf parameter in the find: It's machine specific, so it doesn't work on Linux.

The printf command does exist the printf specifiers are different, try:

printf "%(%Y/%m/%d %H:%M)T\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top