LinuxQuestions.org
Visit Jeremy's Blog.
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 04-20-2017, 12:38 PM   #1
Dickster2
LQ Newbie
 
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Blog Entries: 1

Rep: Reputation: Disabled
How to extract complete filenames from "ls -l" output.


I needed to eliminate "symlinks" from the list of files, and then store only the filename in a file used by "tar" to create an archive. I'm already at the directory I wish to "tar", so all I had to do was this:

ls -l | grep -v '^l' | awk '{$1=$2=$3=$4=$5=$6=$7=$8="";print $0}' | xargs -L1 | grep -v '^$' >/tmp/xfiles

I then used /tmp/xfiles as the tar-control file for creating the tar-file. The initial grep eliminates the symlinks, and the final grep eliminates any empty lines, such as those created at the start of the "ls -l" output. I didn't need to use the -a option. That would have taken more work to clean up the file list.

Lastly, I should elaborate on the "symlink" problem. In Linux (Ubuntu), it's possible to have a lower-case or mixed-case symlink pointing to an all-UPPER-case filename, in the same directory. Creating a tar-file from that directory, and moving it to a Mac-system ends up with a tar-file you can't unzip because Macs are case-blind when it comes to filenames, so the original file AND the symlink created a conflict which makes it virtually impossible to unzip the tar-file. Therefore, I had to eliminate all symlinks while making the tar-file.

I must credit Tinkster for the awk-code, which made this possible. Having the full "ls -l" output allows you to pre-process it based upon information in the first 8 fields, and then eliminate those fields to retain just the filenames.

Last edited by Dickster2; 04-20-2017 at 12:39 PM.
 
Old 04-20-2017, 01:50 PM   #2
serafean
Member
 
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 990
Blog Entries: 15

Rep: Reputation: 135Reputation: 135
Great you found your solution, but wouldn't
Code:
$ find \( ! -type l \) > /tmp/xfiles
be easier ?

Also note that your solution breaks if a filename contains a newline (the only disallowed character in filenames is \0)

Also if it is only for your use, OSX allows enabling HFS case-sensitive mode. Don't know how to do it though.
 
1 members found this post helpful.
Old 04-20-2017, 02:31 PM   #3
Dickster2
LQ Newbie
 
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
To serafean, Ya, that works, but it leaves filenames with leading "./", and it doesn't allow other pre-processing, such as "grep 'Apr 20'" to restrict by date. None of my files ever have an embedded newline character, so I wasn't worries about that aspect. But I do have files with one or more blanks in their names, and I wanted to capture them. Finally, if I don't eliminate the "symlinks", they are included in the output, which could affect 'tar'.

Last edited by Dickster2; 04-20-2017 at 02:33 PM.
 
Old 04-20-2017, 05:39 PM   #4
serafean
Member
 
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 990
Blog Entries: 15

Rep: Reputation: 135Reputation: 135
I suggest you take a look at the find man page for such operations. It saved me quite some headaches
Code:
find \( ! -type l \) -mtime 0 -printf "%P\n"
 
Old 04-20-2017, 11:43 PM   #5
Dickster2
LQ Newbie
 
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
serafean, that's good to know. But on my Mac, find doesn't have a -printf operation.
 
  


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
[SOLVED] Bash - Capture "Child" script output with "Parent" without using files or coproc keyword Jason_25 Programming 2 02-14-2016 07:51 PM
square brackets in output of "ps aux" not matching output of "ps -ejH" alirezan1 Linux - Newbie 14 07-14-2010 04:17 AM
"failed to execute child process" "Input/output error" fl.bratu Fedora 4 12-15-2008 04:03 AM
Regular expression to extract "y" from "abc/x.y.z" rag84dec Linux - Newbie 1 05-29-2008 02:47 AM
How to deal with filenames starting with "-" or "--"? [KIA]aze Linux - Newbie 2 07-08-2007 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:31 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
Open Source Consulting | Domain Registration