LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script help - add absolute path to list of filenames (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-add-absolute-path-to-list-of-filenames-656943/)

greengrocer 07-20-2008 02:00 AM

script help - add absolute path to list of filenames
 
Hello everyone,

I am hoping that someone could take a few minutes to help me, and possibly save me hours of trawling the web.

I have made a list of filenames by doing the following command:

Code:

ls > list.txt
What I would like to do, is add a path to the front of the filenames, or even add the path in real time when I do "ls > list.txt".

I would like to be able to set an absolute value for the path and add it to the front of the file name in the list, so I guess I would need to declare a variable with the path.

Does anybody know of a good way to go about this?

Many thanks in advance,
Greenie

/dev/me 07-20-2008 02:13 AM

The easy way is to just loop

Code:

for i in `ls` ; do
echo `pwd`/$i >> file
done


comm2k 07-20-2008 02:15 AM

Or use find
Code:

find /path/you/want -iname "*" >output

colucix 07-20-2008 02:20 AM

Quote:

Originally Posted by /dev/me (Post 3220402)
The easy way is to just loop

Code:

for i in `ls` ; do
echo `pwd`/$i >> file
done


Or even easier
Code:

ls -d $PWD/* > list.txt
;)

greengrocer 10-11-2008 10:40 PM

Thanks for all the suggestions. I liked

Code:

ls -d $PWD/* > filname.txt
I have found myself trying to use the above command but have it list the filenames with their path on each line in the text files and make the command work recursively through sub directories.

I tried some combinations of adding -R and removing -d but I could not get the desired result. Any suggestions on how to do this correctly and make it recursive through sub directories?

chrism01 10-11-2008 10:51 PM

In that case you want comm2k's soln.

greengrocer 10-11-2008 10:57 PM

Hi Chris,

Yes Comm2K's suggestion is close, is there any way of omitting entries that are just directory names? so I get only filenames?

Maybe there is a way to filter down to *.mpg or something?

colucix 10-12-2008 04:23 AM

If you want to filter file names unsing find, just put the proper pattern to the -name or -iname test. If you want to exclude directory entries from the list, just use -type f to search for regular files only. For example, following the suggestion by comm2k
Code:

find /path/to/some/dir -iname "*.mpg" > filename.txt
will find all the files and directories whose name terminates with .mpg (or some other combination of lower and upper case characters, since -iname stats for ignore case).
Code:

find /path/to/some/dir -type f > filename.txt
will find all the regular files under /path/to/some/dir. You can combine the two tests together if necessary.


All times are GMT -5. The time now is 02:03 PM.