2

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.

Michael Mao
  • 9,878
  • 23
  • 75
  • 91
  • The question is not clear -- what *is* correct? Should each line of the final output be of form `./dir/afile.txt`? Basenames only? – Charles Duffy Apr 14 '10 at 03:52
  • Have you tried the `find` command? – too much php Apr 14 '10 at 03:54
  • 1
    The main obvious thing wrong with the provided answer is that the output file should be '$HOME/lsout.txt'; most people's HOME is not in /home/bqiu. The main thing wrong with what you are doing seems to be that you are ignoring the request to list the contents of /var/log. – Jonathan Leffler Apr 14 '10 at 03:59
  • @Jonathan Leffler : I thought just play around with my home dir would be safer and better to illustrate the problem – Michael Mao Apr 14 '10 at 04:02
  • Essentially a duplicate of duplicate of http://stackoverflow.com/questions/2154166/how-to-recursively-list-subdirectories-in-bash-without-using-find-or-ls-commands – vladr Apr 14 '10 at 04:02

3 Answers3

4

Try -

find /var/log > ~/lsout.txt

chetan
  • 933
  • 4
  • 11
  • Don't think this is what was meant. Plus, how can you guarantee that your `find` implementation does not leave the home directory? – vladr Apr 14 '10 at 04:01
  • 2
    @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 Apr 14 '10 at 04:11
4

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
  • 65,483
  • 18
  • 129
  • 130
  • @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 Apr 14 '10 at 04:13
1

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.

user307666
  • 59
  • 3