0

I want to read a CSV file using Shell, But for some reason it doesn't work.

I use this to locate the latest added csv file in my csv folder

lastCSV=$(ls -t csv-output/ | head -1)

and this to count the lines.

wc -l $lastCSV

Output

wc: drupal_site_livinglab.csv: No such file or directory

If I echo the file it says: drupal_site_livinglab.csv

1 Answer 1

1

Your issue is that you're one directory up from the path you are trying to read. The quick fix would be wc -l "csv-output/$lastCSV".

Bear in mind that parsing ls -t though convenient, isn't completely robust, so you should consider something like this to protect you from awkward file names:

last_csv=$(find csv-output/ -mindepth 1 -maxdepth 1 -printf '%T@\t%p\0' |
  sort -znr | head -zn1 | cut -zf2-)
wc -l "$last_csv"
  • GNU find lists all files along with their last modification time, separating the output using null bytes to avoid problems with awkward filenames.
    • if you remove -maxdepth 1, this will become a recursive search
  • GNU sort arranges the files from newest to oldest, with -z to accept null byte-delimited input.
  • GNU head -z returns the first record from the sorted list.
  • GNU cut -z at the end discards the timestamp, leaving you with only the filename.

You can also replace find with stat (again, this assumes that you have GNU coreutils):

last_csv=$(stat csv-output/* --printf '%Y\t%n\0' | sort -znr | head -zn1 | cut -zf2-)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to store the amount of lines into another variable?
If you want to count the number of files, just use files=( csv-output/* ); echo "${#files[@]}".

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.