0

I have one Linux question that I can't solve. I tried to solve it - I also tried to search the forum, but found no clue - or a way to solve it.

Question:

Write commands that count the amount of files in the Vegetables and Fruits folders together. The answer must be saved in a CounterFood file. Several commands are allowed.

After you execute these commands you are prompted to do so:

The total number of characters in the names of the files in the Vegetables and Fruits folders must be added to the CounterFood file. Several commands may be used.

Try

I thought about doing an arithmetic insert - but I didn't find such a command.

ls -l . Vegetables Fruits  | egrep -c '^-' >> CounterFood

I tried to count how many files there were - but I couldn't count the two folders together. Then I came up with this - my idea is to access the folders - and find all the files - and then count them

find Vegetables Fruits -type f | wc - >>CounterFood 

The continuation of the question is directly related to the beginning. But the beginning didn't work out for me - I can't understand why.

3
  • Look at the find and wc commands (for both parts) Commented Apr 7, 2020 at 11:10
  • 1
    find Vegetables Fruits -type f | wc - >>CounterFood I tried it: My idea is to access the folders - and find all the files - and then count them Commented Apr 7, 2020 at 11:25
  • man wc and check the flags to control what it outputs Commented Apr 7, 2020 at 13:49

2 Answers 2

0

Part 1

find Vegetables Fruits -type f -printf "x" | wc -c > CounterFood      # Print "x" for each filename and count them

Part 2

If you meant add them then this will work

namelen=$(find Vegetables Fruits -type f -printf "%f" | wc -c)        # Characters in file names
counterfood=$(cat CounterFood)                                        # Previous total

echo $((namelen + counterfood)) > CounterFood                         # Add them together

If you meant append them then this will work

find Vegetables Fruits -type f -printf "%f" | wc -c >> CounterFood    # Characters in file names
0

Total Number files in both folders :

expr `ls -l Vegetables | sed 1d | wc -l` + `ls -l Fruits | sed 1d | wc -l` >> CounterFood

Total Number of characters of filenames in both folders :

expr `ls Vegetables | wc -c` + `ls Fruits  | wc -c` >> CounterFoods

Hope this will help!

You must log in to answer this question.

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