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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-20-2017, 12:38 PM
|
#1
|
LQ Newbie
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Rep:
|
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.
|
|
|
04-20-2017, 01:50 PM
|
#2
|
Member
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 997
Rep:
|
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.
|
04-20-2017, 02:31 PM
|
#3
|
LQ Newbie
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Original Poster
Rep:
|
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.
|
|
|
04-20-2017, 05:39 PM
|
#4
|
Member
Registered: Mar 2006
Location: Czech Republic
Distribution: Gentoo, Chakra
Posts: 997
Rep:
|
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"
|
|
|
04-20-2017, 11:43 PM
|
#5
|
LQ Newbie
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Original Poster
Rep:
|
serafean, that's good to know. But on my Mac, find doesn't have a -printf operation.
|
|
|
All times are GMT -5. The time now is 10:40 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|