1

I am supposed to write a shell script to which I send a command line argument, which represents a directory. Then I am supposed to echo all the filenames and number of lines for each file from that directory. I am also supposed to only do this for the top 10 files not older than 10 minutes.

find "$1" -cmin -10 -type f | head -10 

This is what I tried for finding the files newer than 10 minutes, but I have gotten some errors. And also I am not sure how to get the names. Thanks in advance for all the help.

My file names are free. The error I get is that the permission is denied, when passing my home directory as the argument. So I don't get the list of names. I am supposed to both get the list of filenames and the number of lines of each of those files.

When I type /bin/bash ss1 /home/ivana into the terminal, I get the following error:

find: '/home/ivana/.cache/dconf': Permission denied

I am using Linux.

8
  • What errors? How free are your file names? Do you have some sort of fixed naming scheme or are they free? If the latter, your command will fail for file names with newlines. What do you mean "get the names"? The output of the command you show will be a list of file names. What more is needed? Please edit your question and clarify.
    – terdon
    May 31, 2017 at 13:31
  • My file names are free. The error I get is that the permission is denied, when passing my home directory as the argument. So I don't get the list of names. I am supposed to both get the list of filenames and the number of lines of each of those files.
    – ivana14
    May 31, 2017 at 13:37
  • OK, please edit your question and explain this. Comments are easy to miss, hard to read and can be deleted without warning. Show the exact error and also show the exact script and the exact command you use to call it. There is no way you don't have permission to read your home dir (that would have stopped you from logging in in the first place) so we need more detail.
    – terdon
    May 31, 2017 at 13:39
  • Do try a loop(for file in $(command); do; name=$(basename $file .*); echo $name | wc -l; done) and btw |head -10 is taking the 10th file, not the first 10 if I'm right
    – ADDB
    May 31, 2017 at 13:40
  • @ADDB I tried that and I get the error which I added to my question.
    – ivana14
    May 31, 2017 at 14:00

1 Answer 1

2

OK, first things first. The error you get is irrelevant, it just means that a specific subdirectory (/home/ivana/.cache/dconf) of your $HOME is not owned by you. This almost certainly happened because you ran a graphical application with sudo. The simple fix is:

sudo chown -R ivana /home/ivana/

That will change the ownership of all files and directories in your $HOME back to your own username. If you can have things there that are correctly owned by another user, then just change the ownership of the dconf directory:

sudo chown -R ivana /home/ivana/.cache/dconf

Now, that said, the way to do this correctly and deal with arbitrary file names is:

find "$1" -cmin -10 -type f -print0 | head -z -n 10 

That will cause find to print each file found with a trailing \0 instead of a newline, which ensures it will work with files whose names contain newline characters. The -z option of head makes it read \0-delimited input.

The next step is to read each of the files and print the number of lines they have:

find "$1" -cmin -10 -type f -print0 | head -z -n 10 |
    while IFS= read -r -d '' file; do wc -l "$file"; done

The IFS= sets the input field separator to empty, the -r makes read not treat \ specially (so \t doesn't become a tab) and the -d '' tells read to read \0-delimited data.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .