tags:

views:

207

answers:

3

Run a recursive listing of all the files in /var/log and redirect standard output to a file called lsout.txt in your home directory. Complete this question WITHOUT leaving your home directory.

An: ls -R /var/log/ > /home/bqiu/lsout.txt

I reckon the above bash command is not correct. Because I found what it stores was :

$ ls -R /var/log
/var/log:
empty.txt  setup.log  setup.log.full  tmp

/var/log/tmp:
fake.txt subfolder

/var/log/tmp/subfolder:

Does that mean problem resolved?

I reckon NOT.

Because it contains more "stuff" than "only files"

Or at least, if the purpose was to locate all "files" underneath the "/var/log" directory recursively, then I hope to get the anwser like this:

/var/log/empty.txt  
/var/log/setup.log  
/var/log/setup.log.full
/var/log/tmp/fake.txt

So then someone can parse the content of the output for later use. Such like

$ perl -wnle 'print "$. :" , $_;' logfiles
1 :/var/log/empty.txt
2 :/var/log/setup.log
3 :/var/log/setup.log.full
4 :/var/log/tmp/fake.txt

This is what I've got so far:

$ ls -1R
.:
cal.sh
cokemachine.sh
dir
sort
test.sh

./dir:
afile.txt
file
subdir

./dir/subdir:

$ ls -R | sed s/^.*://g

cal.sh
cokemachine.sh
dir
sort
test.sh


afile.txt
file
subdir

But this still leaves all directory/sub-directory names (dir and subdir), plus a couple of empty newlines

How could I get the correct result without using Perl or awk? Preferably using only basic bash commands(this is just because Perl and awk is out of assessment scope)

Edited : I focused on my own "$HOME" folder just to restrict the file listed. I am having little content in my homedir

Edited 2nd: Sorry about my inapproprated question in the initial form. I fixed the wording and hopefully everyone can see the problem now.

+4  A: 

Try -

find /var/log > ~/lsout.txt

chetan
Don't think this is what was meant. Plus, how can you guarantee that your `find` implementation does not leave the home directory?
vladr
@Vlad: I'd let the op decide what pipit was desired. As for the find command "leaving. The home directory-- I have no idea what you're talking about. Please elaborate.
chetan
+1  A: 

If you were given no restrictions in terms of which commands can or cannot be used, ls -R /var/log >~/lsout.txt or find /var/log -print >"$HOME/lsout.txt" or any similar combination will work just fine.

However, if the point of the assignment is to write a 100% sh-based implementation, without using ls -R, find, etc. then you should be producing something along the lines of:

#!/bin/sh

# Helper method which recursively lists the contents of a given directory
# Usage: recurse_ls target_directory
recurse_ls()
{
  TARGET_DIR="$1"
  # list contents of $TARGET_DIR
  ...
  # - recursive call to list contents of sub-directories
  recurse_ls ...
  ...
}



# MAIN
# Usage: script.sh target_directory
# - check that parameters to script.sh are correct
...
# - list the contents of target_dir and its subdirectories
recurse_ls "$1"

Useful links:

vladr
@Vlad Romascanu :Thanks for this sample skeleton code. This question is left "unfinished" as taken from a collection of multiple choice questions, I reckon. I just felt the "proposed" answer in the past was not quite right.
Michael Mao
@Vlad Romascanu : again, thanks for the reference links!
Michael Mao
+1  A: 

I'd guess that the answer they want is: ls -R /var/log/ > /home/bqiu/lsout.txt
ie. the original answer you said was wrong.

Except you may want to write it as: ls -R /var/log/ > ~/lsout.txt.
That way it outputs to the home directory of whoever is logged in, rather than just user "bqiu".

When it says: Run a recursive listing of all the files
To me ls stands for listing and the -R option stands for recursive.
So to me the wording of the question suggests using ls -R to produce the listing,

But a it depends upon what format they want the listing.