Results 1 to 5 of 5

Thread: Why parsing the output of ls is considered unsafe?

  1. #1
    Is that all you got? rockdalinux's Avatar
    Join Date
    May 2005
    Location
    Planet Vegeta
    Posts
    987
    Thanks
    27
    Thanked 71 Times in 62 Posts
    Rep Power
    19

    Default Why parsing the output of ls is considered unsafe?

    This one bothers me a lot why ls command output is considred as unsafe?
    Code:
    ls -l | something
    ls -l | command | do | get_output
    Rocky Jr.
    What's wrong? I hope I am not making you uncomfortable...

    Never send a boy to do a mans job.

  2. #2
    Senior Member
    Join Date
    Sep 2006
    Posts
    131
    Thanks
    0
    Thanked 33 Times in 29 Posts
    Rep Power
    11

    Default

    Quote Originally Posted by rockdalinux View Post
    This one bothers me a lot why ls command output is considred as unsafe?
    Code:
    ls -l | something
    ls -l | command | do | get_output
    it will NOT be "unsafe" if you know what you are doing. If you are parsing files, don't use ls -l because you might hit problems with files with spaces in their file names.

    a lot depends also on what your "something" or "command" is.... what do you actually want to do?? describe the problem you are having.

  3. The Following 2 Users Say Thank You to ghostdog74 For This Useful Post:

    rockdalinux (1st January 2010), vamsi (30th December 2009)

  4. #3
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,498
    Thanks
    17
    Thanked 788 Times in 506 Posts
    Rep Power
    10

    Default

    The ls will display all files including newlines, commas, pipes and almost anything except for NUL (\0). By default ls separates its output with newline (\n). This is good, but imagine your filename has newline (\n) itself. This makes it pretty difficult to get filename safely, hence it is considered as unsafe. Also, some output fields in the ls command changes from UNIX to Linux oses. Try using while or for loop to get file list. Here is a common for loop I use (again as ghostdog74 said provide us more info as we cannot imagine your problem ):

    Code:
    find /path/to/dir -type f -print0 | while IFS= read -r -d '' file
    do
      echo "Processing ${file}
    done
    While example:
    Code:
    find . -print0 | while read -d $'\0' file
    do
      echo -v "$file"
    done
    See http://www.cyberciti.biz/tips/handli...s-in-bash.html
    Last edited by nixcraft; 31st December 2009 at 09:50 AM.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  5. The Following User Says Thank You to nixcraft For This Useful Post:

    rockdalinux (1st January 2010)

  6. #4
    Is that all you got? rockdalinux's Avatar
    Join Date
    May 2005
    Location
    Planet Vegeta
    Posts
    987
    Thanks
    27
    Thanked 71 Times in 62 Posts
    Rep Power
    19

    Thumbs up

    I never had problem coz I never had a blank or anything special in file name. But just now I tested with blank and other stuff and it did failed. I was comparing some file using ls and sending to our program written in python. thanks for sharing for and while loop. I will patch my script later on
    Rocky Jr.
    What's wrong? I hope I am not making you uncomfortable...

    Never send a boy to do a mans job.

  7. #5
    Senior Member cfajohnson's Avatar
    Join Date
    May 2009
    Posts
    190
    Thanks
    0
    Thanked 44 Times in 36 Posts
    Rep Power
    9

    Default Parsing output of ls -l

    There is no problem parsing the output of ls -l unless there is a newline in a filename. Whitespace is no problem.

    Symlinks need to be treated specially.

    Code:
    ls -l |
     while read -r perms links owner group size month day time file
     do
       case $file in
         *\ -\>\ *) : parse link name and file name here ;;
       esac
       : do whatever
     done

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Solved] Record command output
    By asim.mcp in forum Shell scripting
    Replies: 6
    Last Post: 11th October 2009, 11:49 PM
  2. How to group the output w/ limit
    By alvingo in forum Shell scripting
    Replies: 1
    Last Post: 26th July 2009, 05:45 PM
  3. How to group the output of a loop
    By alvingo in forum Shell scripting
    Replies: 3
    Last Post: 16th July 2009, 09:18 AM
  4. About iptables output
    By satimis in forum Networking, Firewalls and Security
    Replies: 0
    Last Post: 30th September 2007, 08:30 AM
  5. How to redirect Output
    By puppen in forum Shell scripting
    Replies: 15
    Last Post: 6th December 2006, 08:44 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •