Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-15-2011, 06:46 PM   #1
LQ Newbie
Registered: Dec 2010
Posts: 16

Rep: Reputation: 0
the ins and outs of "ls"

First off, I should say that I posted a similar query about pathnames (specifically how to remove them) on linuxquestions quite recently and it was helpfully answered. The following question is a little different....:

I am have trouble getting the ls command to work exactly how I want, for example, sometimes I want to list the contents of a directory into a text file by command:
ls directory > list.txt
(assume the only objects the directory contains are files with extension .dat eg file1.dat, file2.dat, ...)sometimes I want the relative path of the files prepended to them in my text file eg:


other times I want just the file name and not the path, eg.:


It feels like ls is unpredictable in this regard - sometimes it prepends the path and sometimes it doesn't and sometimes it adds both entries (with and without the path prepended into the text file)....How can better control the way ls works?
Old 06-15-2011, 06:58 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,565

Rep: Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425
'ls' is not what you want for the dir prefix; try 'find'.
See the docs
Another option is to write a short script, possibly based on 'ls -R'
Old 06-15-2011, 07:00 PM   #3
Registered: May 2001
Posts: 29,393
Blog Entries: 55

Rep: Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565Reputation: 3565
I agree: with "-printf" its output is configurable in ways 'ls' can't provide you with.


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
What distro is best for learning the ins and outs? adunamia Linux - General 8 12-18-2009 07:28 PM
LXer: The ins and outs of picking the best XML Parser LXer Syndicated Linux News 0 01-14-2007 03:33 PM
New to ubuntu, can someone give me the ins and outs of apt? enigma_0Z Ubuntu 2 05-25-2005 12:19 PM
"opt/"can not be read?? masterguardian Linux - Software 1 08-07-2004 01:02 PM
Linux expert needed that knows INS & OUTS! Milkman00 Slackware 16 08-09-2002 06:03 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:09 AM.

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