-4

I have to make a script which prints the sizes of all .conf files from /etc recursively in a specified file, and that prints the errors caused by the lack of permission in another file. I don't know how to make it. Any suggestion would be very helpful.

Eliah Kagan
  • 117,780
  • 2
    Wrong website to ask this. We are not a coding service nor is coding a focus of askubuntu. "I need to make a script" eh. What you ask reads more like a one-liner with redirects to a file. – Rinzwind Nov 16 '19 at 18:23
  • What is the "memory" of a conf file? Do you mean the file size? – glenn jackman Nov 16 '19 at 18:35
  • 1
    I was thinking the same but why would that result in permissions errors :D – Rinzwind Nov 16 '19 at 19:00
  • sorry. it was a mistake. i meant the size. and yes, i was referring to a oneliner – Student 1234 Nov 16 '19 at 19:39
  • "all files recursively" -- use find. find's -printf directive can print the file name, size and permissions. – glenn jackman Nov 16 '19 at 19:49
  • @Rinzwind We do answer easy student homework questions here frequently. If a consultant charging for services ask a complicated question (requiring many hours of work) in that case I don't answer. – WinEunuuchs2Unix Nov 17 '19 at 14:09
  • 3
    Closer Voters: The question is not off-topic because it involves find and printf command which is common in Ask Ubuntu. Nor can this be considered Too Broad nor Unclear IMHO. – WinEunuuchs2Unix Nov 17 '19 at 14:41
  • @user68186 The version of Linux Kernel, or the version of Ubuntu really doesn't effect answers here. Did you vote question as unclear as a consequence? – WinEunuuchs2Unix Nov 17 '19 at 14:46
  • @glennjackman In this context I would interpret "memory" as "Non-Volatile Memory" aka "Non-Volatile Storage" so the size on disk as you accurately suggested. Most people associate "memory" with RAM (Random Access Memory) but technically it applies to ROM, HDD and SSD as well. If you voted Unclear will you now retract the vote? – WinEunuuchs2Unix Nov 17 '19 at 14:50
  • 3
    @WinEunuuchs2Unix not when asking for scripts though. Help with one-liners where someone gets stuck sure But -this- shows zero effort. – Rinzwind Nov 17 '19 at 16:44
  • @Rinzwind I agree with you on effort. But I recall my college days when IBM PC's were first invented and that BASIC instructor was so useless (perhaps because of his mainframe background) us students were pretty much left teaching each other. I have to give OP benefit of doubt their plight was similar to ours. – WinEunuuchs2Unix Nov 17 '19 at 17:23
  • 1
    @WinEunuuchs2Unix I agree the version of Ubuntu should not matter. However, if the OP is running an old EOL version or a non-Ubuntu distro, the question would be off-topic. If OP tells us she is not running Ubuntu or running an EOL version, I will vote to close on that basis. – user68186 Nov 17 '19 at 17:43
  • @user68186 Please don't refer to OP as "she". Some might construe this comment as stating females have more problems with technology than males. Kindly use the phrase "they" instead of "she". Thank you. Also even if OP was running Linux Mint I would still answer the question because it applies to all Ubuntu versions too. Including Ubuntu version EOL. – WinEunuuchs2Unix Nov 17 '19 at 17:50
  • @WinEunuuchs2Unix I am with Rinzwind. I answer script questions if I know how. However, OP at least has to show some effort in the form of the non-working script (one liner in this case) in the question. If not I will be happy to vote to close on the basis of Mint or EOL version. BTW, I used "she" to break the stereotype that all computer script users are male. I agree with you a gender neutral pronoun is more appropriate. Next time I will use they. – user68186 Nov 17 '19 at 18:20
  • @Student1234 Please clarify: Which operating system and version do you use? Which permission errors are you referring to? Do you need the individual sizes or the total size of these files? What have you tried, and where did it fail? – dessert Nov 17 '19 at 18:26
  • hi. i use ubuntu 18.04 lts. The errors are caused by the lack of permission because i am not allowed to use the terminal as root. I tryed it and i couldn't find ta way to get the files that printed errors separated from the files that don't. Thank you all for your help. All the things helped me. I think it is ok now. – Student 1234 Nov 17 '19 at 18:36
  • 1
    Please add new information directly to your question ([edit]), do not use comments. – dessert Nov 17 '19 at 19:24

3 Answers3

1

You can use -fprintf to write output from find to a file.

$ find /etc -type f,l -name '*.conf' \
    -fprintf ~/filelist '%s %p\n' 2> ~/error.log

find options:

  • -type f,l Search for regular files and symbolic link.
  • -name Match the base of the file name with shell match pattern.
  • -fprintf Like -printf but writes output to a file instead.

fprintf format:

  • %s File's size in bytes.
  • %p File's name (with starting-point included).
  • \n Newline.

Bash:

  • [n]>file Redirect file descriptor [n] to "file".
  • ~ Expands to the value of the shell parameter HOME.
  • \ Breaks a up long lines into multiple lines.
  • 1
    Personally I think answer is better without -L because it is doubling up on size of /etc/*.conf files and becomes misleading IMO. I've shown the differences between your answer and my own below. – WinEunuuchs2Unix Nov 17 '19 at 14:54
  • OP seems interested in permissions too. Might want to add %m or %M – glenn jackman Nov 17 '19 at 15:43
0

As mentioned in comment find and printf are your friends on this homework assignment. For a one-liner use:

find /etc -name "*.conf" -printf "%s %p\n" 1>namesize.txt 2>errors.txt

To test the components with output to your screen try out the examples below.

$ find /etc -name "*.conf" 1>/dev/null
find: ‘/etc/ssmtp’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/ssl/private’: Permission denied

This is the short one that shows permission denied. Regular output is redirected to /dev/null (nowhere) so you get to see only the error messges.

$ find /etc -name "*.conf" -printf "%s %p\n" 2>/dev/null | column -t
3874   /etc/bluetooth/main.conf
258    /etc/bluetooth/proximity.conf
397    /etc/bluetooth/input.conf
  (... SNIP ...)
1366   /etc/at-spi2/accessibility.conf
13592  /etc/openal/alsoft.conf
1800   /etc/cracklib/cracklib.conf

This shows the the list of filenamnes and sizes which is too long to print entirely. The four error messages for "Permission denied" are filtered out with 2>/dev/null redirecting all errors to "nowhere".

Output is piped (using |) to the column -t command which pads spaces behind the file sizes in order for the filenames to align in the output.


Compare links to other answer

The other answer shows links like this:

$ find -L /etc -type f -name '*.conf' -fprintf ~/filelist '%s %p\n' 2> ~/error.log
$ grep 45-latin.conf ~/filelist | column -t
4621  /etc/fonts/conf.d/45-latin.conf
4621  /etc/fonts/conf.avail/45-latin.conf

The size of the link inherits the size of the file it points to.

My answer shows links like this:

$ find /etc -name "*.conf" -printf "%s %p\n" 1>namesize.txt 2>errors.txt
$ grep 45-latin.conf namesize.txt | column -t
27    /etc/fonts/conf.d/45-latin.conf
4621  /etc/fonts/conf.avail/45-latin.conf

The size of the link is the real size of the link (27 bytes).

So it's a question of how your professor/TA/teacher wishes to links reported in the answer. The homework/Lab assignment should have been more specific on how to handle such intricacies of Linux.

To look at the link use:

$ ll /etc/fonts/conf.d/45-latin.conf
lrwxrwxrwx 1 root root 27 Aug  2  2018 /etc/fonts/conf.d/45-latin.conf -> ../conf.avail/45-latin.conf
0

You can use bash’s globstar option:

If set, the pattern ** used in a pathname expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.

Set it with:

shopt -s globstar

After that you can match any .conf file under /etc with /etc/{,**/}*.conf, so now a simple stat call is enough to solve the task, e. g. for size and filename separated by space:

stat -c '%s %N' /etc/{,**/}*.conf

To redirect the output of that, use >file for stdout and 2>file for stderr, e. g.:

stat -c '%s %N' /etc/{,**/}*.conf >~/stdout 2>~/stderr

As the glob doesn’t match any directory for which you don’t have the proper permissions you normally don’t get any permission errors though, but those files are just skipped instead. To list them you can use find and search for matching files not matching the o+r permission, i. e. not readable by others:

sudo find /etc -type f -name '*.conf' ! -perm -o+r

Further reading

dessert
  • 39,982
  • I was thinking a student wouldn't have sudo powers but I haven't been in a School's Linux Computer Lab so could not say. – WinEunuuchs2Unix Nov 17 '19 at 14:03
  • it was my bad.. missed globstar... –  Nov 17 '19 at 16:26
  • You can't use "sudo" as per question: "and that prints the errors caused by the lack of permission in another file. " – Rinzwind Nov 17 '19 at 16:44
  • @Rinzwind the question is very unclear in this regard, my stat approach does what OP asks for without throwing any permission errors – see last paragraph – dessert Nov 17 '19 at 17:23
  • @Rinzwind I doubt there is actually a way to show permission errors for files in directories you’re not allowed to read – note that the errors find reports do not refer to the conf files, but to directories find can’t access in general, which is not what OP asked for either. Take /etc/sudoers.d for example, it shows up although irrelevant. – dessert Nov 17 '19 at 18:00