Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash command: head

I am trying to find all files with dummy* in the folder named dummy. Then I need to sort them according to time of creation and get the 1st 10 files. The command I am trying is:

find -L /home/myname/dummy/dummy* -maxdepth 0 -type f -printf '%T@ %p\n' | sort -n | cut -d' ' -f 2- | head -n 10 -exec readlink -f {} \;

But this doesn’t seem to work with the following error:

head: invalid option -- 'e'
Try 'head --help' for more information.

How do I make the bash to not read -exec as part of head command?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

UPDATE1:

Tried the following:

find -L /home/nutanix/dummy/dummy* -maxdepth 0 -type f -exec readlink -f {} \; -printf '%T@ %p\n' | sort -n | cut -d' ' -f 2- | head -n 10

But this is not according to timestamp sort because both find and printf are printing the files and sort is sorting them all together.

Files in dummy are as follows:
dummy1, dummy2, dummy3 etc. This is the order in which they are created.

>Solution :

How do I make the bash to not read -exec as part of head command?

The -exec and subsequent arguments appear intended to be directed to find. The find command stops at the first |, so you would need to move those arguments ahead of that:

find -L /home/myname/dummy/dummy* -maxdepth 0 -type f -printf '%T@ %p\n' -exec readlink -f {} \; | sort -n | cut -d' ' -f 2- | head -n 10

However, it doesn’t make much sense to both -printf file details and -exec readlink the results. Possibly you wanted to run readlink on each filename that makes it past head, in which case you might want to look into the xargs command, which serves exactly the purpose of converting data read from the standard input into arguments to a command. For example:

find -L /home/myname/dummy/dummy* -maxdepth 0 -type f -printf '%T@ %p\n' |
  sort -n |
  cut -d' ' -f 2- |
  head -n 10 |
  xargs -rd '\n' readlink -f
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading