AaronMR Computer Engineering

26Mar/100

List of all files on the PC -> to a file .. in Mac and Linux

Already current in the previous post why I needed to do this, the question is that I did in Windows because it is what is used here. In this post we will see how to do the same thing in Linux and Mac that is what I most often use.

I have been researching and the command tree is not for those OSes, or at least I have not found so we will get the same thing with the tree but using a combination of commands and parameters.

Code and example after the jump ->

It must be said that there is a find command that besides looking for a file and do some action, we returned hierarchical directory structure consultation, but we return a tree structure similar to Windows.

$ find prueba/

prueba/
prueba//maki1
prueba//maki1/sfsdfsdfsdfdsf
prueba//maki2
prueba//maki3
...

While surfing the Internet I found a chained series of commands that generates what we want, this command is as follows:

$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'

It is a piece of command but this will generate the following:

|-maki1
|---maki1_1
|-----maki1_1_1
|---sfsdfsdfsdfdsf
|-maki2
|-maki3
...

Let us explain a little more than is what is the command and that is composed and how it makes everything you do.

The first thing to realize that they are actually 3 commands that are connected by means of pipes "|", these three commands are responsible for:

ls -R -> show a listing of the contents of a directory, the-R is to give recursion, ie among all the directories that hang from the directory where you are.

grep ":$" -> allows us to find certain strings or patterns in a text file. In this case all those who have the name of the directory in which we are conducting the search.

sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' -> is a command that allows us to edit text strings from an input file. The parameter-e is to run a script, which is what is below and should have the form 's / regexp / replacement /' ie, the expression regexp = we are looking to replace, replacement = expression that going to get when we replace regexp.

Now if we keep what we generate those commands to a file, simply add "> filename" and we already have the system ready to save the directory tree in a file.

$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' > lista_directorio.txt

Greetings to all and hope you can be useful ....

PS: Keep in mind the issue of permits and all that, if you want to do this from the root probably going to have to do with permissions superususario, namely:

$ sudo ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' > lista_directorio.txt

Bye bye...

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.