LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-07-2010, 03:27 PM   #1
chudster
LQ Newbie
 
Registered: May 2008
Posts: 22

Rep: Reputation: 0
Question Using cut on file full of ls -l output to display only filenames


I have a file that contains "ls -la" output. I would like to display only the filenames, none of the other information before it such as permissions, ownership, size, and date.
Would the cut command be the best way to hit this, or should I use Vim or sed?

Thanks.
 
Old 10-07-2010, 03:31 PM   #2
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 23
You mean like:

Code:
ls -la | awk '{print $9}'
what's wrong with

Code:
ls -a
You need every filename on a separate line?
 
1 members found this post helpful.
Old 10-07-2010, 06:23 PM   #3
chudster
LQ Newbie
 
Registered: May 2008
Posts: 22

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by suprstar View Post
You mean like:

Code:
ls -la | awk '{print $9}'
That's close, but the ls -la output is already in a file. So I just want to run awk against that file and strip out everything before the filename (the 9th field) on each line.
Edit: Looks like this will work: awk '{print $9}' file > newfile

Last edited by chudster; 10-07-2010 at 06:27 PM.
 
Old 10-11-2010, 01:43 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by chudster View Post
That's close, but the ls -la output is already in a file. So I just want to run awk against that file and strip out everything before the filename (the 9th field) on each line.
Edit: Looks like this will work: awk '{print $9}' file > newfile
It will if you happen not to have any files with spaces in
their names.... otherwise a slightly more convoluted approach
may work:
Code:
awk '{$1=$2=$3=$4=$5=$6=$7=$8="";print $0}' file

Cheers,
Tink
 
Old 10-11-2010, 02:16 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Gosh, if you don't need the other stuff but just the file names, just do
Code:
 ls -a
??
 
Old 10-11-2010, 01:52 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by ghostdog74 View Post
Gosh, if you don't need the other stuff but just the file names, just do
Code:
 ls -a
??
Quote:
Originally Posted by chudster View Post
That's close, but the ls -la output is already in a file.

Cheers,
Tink

Last edited by Tinkster; 10-11-2010 at 01:54 PM.
 
Old 10-11-2010, 08:53 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Tinkster View Post
Cheers,
Tink
i was looking at his first post.
then the better way to solve this (if possible) is to go to the source and change ls -la to ls -a.

Last edited by ghostdog74; 10-11-2010 at 08:56 PM.
 
Old 04-17-2017, 07:44 PM   #8
Dickster2
LQ Newbie
 
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Blog Entries: 1

Rep: Reputation: Disabled
how to get the fulll filename from ls -l

I very much liked Tinkster's method using awk. In my case, I needed to eliminate "symlinks" from the list of files, and then store the filename only 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 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 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 zip-file. Therefore, I had to eliminate the conflicting symlink while making the zip-file.

Last edited by Dickster2; 04-18-2017 at 10:54 AM.
 
Old 04-18-2017, 02:33 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,918

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
in general you must not use grep|awk|sed|grep (or similar) chains, it can be usually solved by a single awk/perl/python/whatever script.
In your case for example:
Code:
# instead of
grep -v '^l'
# awk can be used:
awk '/^l/ { next }'

# instead of
grep -v '^$'
# you can use
/^$/ { next }

# instead of sed
# you can try gsub
so in general you can try something like this:
Code:
ls -l | awk ' /^l/ { next }
              /^\s*$/ { next }
              { print $9 }' > /tmp/xfiles
 
Old 04-18-2017, 08:18 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206Reputation: 1206
With shell built-ins
Code:
while read x x x x x x x x x; do echo "$x"; done < file
A space in the filename is no problem.
 
Old 04-18-2017, 09:13 AM   #11
Dickster2
LQ Newbie
 
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Blog Entries: 1

Rep: Reputation: Disabled
The suggestion from pan64 fails in two ways: 1) it leaves a blank line caused by the "Total xxxxx" at the start of "ls -l", and 2) it only captures the first non-blank portion of filenames with embedded blanks.

The suggestion from MadeInGermany is clever, if what's in 'file' is the output from "ls -l". It reads the first eight parts and discards them, and leaves me with whats left, but it also leaves me with a blank line from "Total xxxxx".

What I need to save is the complete filename following the first eight fields in "ls -l", with no leading blanks and no blank lines. I need to use "ls -l" to eliminate "symlinks" with "grep -v '^l'", then extract the complete filenames. MadeInGermany came close, but I can't hand the 'file' to 'tar'.

Last edited by Dickster2; 04-18-2017 at 09:29 AM.
 
Old 04-18-2017, 09:23 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,918

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
Quote:
Originally Posted by Dickster2 View Post
The suggestion from pan64 fails in two ways: 1) it leaves a blank line caused by the "Total xxxxx" at the start of "ls -l", and 2) it only captures the first non-blank portion of filenames with embedded blanks.
I did not want to give you a full solution (and actually I do not know what do you really need), but I gave you an idea which can be easily extended to fulfil all your needs.

Quote:
Originally Posted by Dickster2 View Post
The suggestion from MadeInGermany makes no sense. What's in the "file"? If it's the output from "ls -l", then it reads the eight parts that I want discarded from "ls -l", and leaves me with just the first non-blank portion of filenames with embedded blanks.
Probably you do not understand it, but it does make sense. file actually is a file, especially this is the file you were talking about
Quote:
I have a file that contains "ls -la" output.
and the tip he gave will print the filenames.

Quote:
Originally Posted by Dickster2 View Post
What I need to save is the complete filename following the first eight fields in "ls -l", with no leading blanks and no blank lines. I need to use "ls -l" to eliminate "symlinks" with "grep -v '^l'", then extract the complete filenames.
So you have got 2 ideas to improve
 
Old 04-18-2017, 05:03 PM   #13
Dickster2
LQ Newbie
 
Registered: Jul 2016
Location: Mountain View, CA
Posts: 23
Blog Entries: 1

Rep: Reputation: Disabled
I edited my 5:44pm post from yesterday, and added more explanation at 7:13am today. Anyone interested should revisit.
 
Old 04-19-2017, 12:20 AM   #14
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Dickster2 View Post
I edited my 5:44pm post from yesterday, and added more explanation at 7:13am today. Anyone interested should revisit.
this is very confusing; you're disrupting the flow of the thread.
if you have something to add, please add it below this post.
 
Old 04-19-2017, 05:33 AM   #15
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Given its already in a file, I'd do it in vim, but then I'm just crazy enough to enjoy that sort of thing
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
How-to : get full path of each file after applying find and cut kaprasanna Linux - General 1 01-07-2008 01:19 AM
floppy and CD disk filenames cut off Cinematography Linux - Software 13 05-27-2005 03:56 PM
Windows unable to read full filenames from CD burnt on linux wearetheborg Linux - Software 6 09-14-2004 06:40 PM
'cat' not allowing absolute filenames when piped to 'cut' d3funct Programming 6 12-06-2002 02:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:54 AM.

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