LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-20-2008, 03:00 AM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
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
 
Old 07-20-2008, 03:13 AM   #2
/dev/me
Member
 
Registered: May 2008
Distribution: Slackware 13
Posts: 116

Rep: Reputation: 20
The easy way is to just loop

Code:
for i in `ls` ; do
echo `pwd`/$i >> file
done
 
Old 07-20-2008, 03:15 AM   #3
comm2k
LQ Newbie
 
Registered: Jul 2008
Posts: 28

Rep: Reputation: 16
Or use find
Code:
find /path/you/want -iname "*" >output
 
Old 07-20-2008, 03:20 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by /dev/me View Post
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
 
Old 10-11-2008, 11:40 PM   #5
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
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?
 
Old 10-11-2008, 11:51 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
In that case you want comm2k's soln.
 
Old 10-11-2008, 11:57 PM   #7
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
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?
 
Old 10-12-2008, 05:23 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to list the filenames which are in lower case naveensankineni Programming 2 03-12-2008 08:09 AM
TFTP Error #2: Only absolute filenames allowed + remove regular file VampireL0rd Fedora 1 07-19-2007 04:37 PM
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 08:45 AM
how do i add gcc to the Path list? wonderwizard SUSE / openSUSE 6 02-18-2006 01:14 AM
'cat' not allowing absolute filenames when piped to 'cut' d3funct Programming 6 12-06-2002 03:36 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration