2

How would I make the following less redundant? That is, is there a way to combine the two almost-identical statements into one?

FULFILLMENT="/Users/david/Desktop/pds" # "/Volumes/FulfilmentArray/"
ARCH1="/Users/david/Desktop/etc" # "/Volumes/Arch_01/"

FILE="/tmp/files.txt"

# find all the paths and print them to a file
sudo find $FULFILLMENT -ls | python -c '
import sys
for line in sys.stdin:
    r = line.strip("\n").split(None, 10)
    fn = r.pop()
    print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\""
' > $FILE &&

sudo find $ARCH1 -ls | python -c '
import sys
for line in sys.stdin:
    r = line.strip("\n").split(None, 10)
    fn = r.pop()
    print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\""
' >> $FILE

3 Answers 3

4

Find can go through multiple directories in a single command:

FULFILLMENT="/Users/david/Desktop/pds" # "/Volumes/FulfilmentArray/"
ARCH1="/Users/david/Desktop/etc" # "/Volumes/Arch_01/"

FILE="/tmp/files.txt"

# find all the paths and print them to a file
sudo find "$FULFILLMENT" "$ARCH1" -ls | python -c '
import sys
for line in sys.stdin:
    r = line.strip("\n").split(None, 10)
    fn = r.pop()
    print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\""
' > $FILE 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, and this will do a recursive find on all the subdirectories in the main directory as well?
Yes, it will go through every dir under $FULFILLMENT and $ARCH1
1

You can do:

cmd='
import sys
for line in sys.stdin:
    r = line.strip("\n").split(None, 10)
    fn = r.pop()
    print ",".join(r) + ",\"" + fn.replace("\"", "\"\"") + "\""
'
sudo find $FULFILLMENT -ls | python -c "$cmd" >> $FILE
sudo find $ARCH1 -ls | python -c "$cmd" >> $FILE

Comments

1

Although the output of the following is slightly different, I suspect you would be happier with:

FULFILLMENT=/Users/david/Desktop/pds
ARCH1=/Users/david/Desktop/etc
exec > /tmp/files.txt

find $FULFILLMENT $ARCH1 -exec stat -c '%i,%b,%A,%h,%U,%G,%y,%n' {} \;

The format of the date is different, and stat may report a different blocksize if you are not invoking find with POSIXLY_CORRECT. This does not attempt to escape quotes in the filename nor to put the filename in quotes since you are clearly not worried about filenames which may contain a comma, so we can assume the output can be parsed reliably as csv and not worry about the quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.