Bash: recursive list of all file problems

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

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

I believe the above bash command is wrong. Because I found that it stores:

$ 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 this mean the problem has been resolved?

I do not think.

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

Or at least if the goal was to find all the "files" under "/ var / log" recursively, then I hope it looks 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 as

$ 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 have 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 that still leaves all the directory / subdirectory names (dir and subdir), plus a couple of blank newlines

How can I get the correct result without using Perl or awk? It is preferable to use only basic bash commands (this is only because Perl and awk are out of scope)

Edited: I focused on my own $ HOME folder to restrict the specified file.

Edited 2nd: Sorry for my unsuccessful question in original form. I have corrected the wording and hope everyone can see the problem now.

+2


a source to share


3 answers


Try -



find / var / log> ~ / lsout.txt

+4


a source


If you were not provided to limit , by means of which the team may or may not 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 assignment point is a record of a 100% set sh

implementation
, no use ls -R

, find

etc., then you must produce something like this:

#!/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:

+4


a source


I would assume they want the answer: ls -R /var/log/ > /home/bqiu/lsout.txt


that is. the original answer you said was wrong.

Except for the fact that you can write it as: ls -R /var/log/ > ~/lsout.txt

.

Thus, it displays the logged in person's home directory, not just the user's "bqiu".

When he says: Run a recursive listing of all the files


For me ls

means listing

, and option -R

means recursive

.
So for me, the formulation of the question suggests using ls -R

to create a list,

But it depends on what format they need.

+1


a source







All Articles