LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2005, 09:28 PM   #1
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
how to extract the name of the file


ok here is what i want to do

i run this command

ls -l |grep "^d"

it gives the complete listing of the directory

what i want is that some how i should be able to extract the names of the directories only that are output at the end of the ls -l command

same goes for the files
 
Old 03-21-2005, 09:38 PM   #2
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Rep: Reputation: 30
im not sure i understan what you mean by extract... but you could put them in a file

ls -l >>somefile

Where did you want to extract them to?
 
Old 03-21-2005, 09:46 PM   #3
papalukg
LQ Newbie
 
Registered: Dec 2003
Location: Patras
Distribution: slack
Posts: 4

Rep: Reputation: 0
Code:
ls -F |grep \/
 
Old 03-21-2005, 10:55 PM   #4
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
by extrating i mean

that when i get the output from the ls -l command i should be able to extract only the file names and not the rest of the rubbish which comes out of it.

bash-3.00$ ls -l
total 2881
drwx------ 2 jnsahibz mc00 512 Mar 14 16:15 CV
drwxr-xr-x 2 jnsahibz mc00 512 Mar 20 22:27 Desktop
drwx------ 3 jnsahibz mc00 512 Feb 17 11:28 Favorites
drwx------ 3 jnsahibz mc00 512 Mar 5 18:14 My Documents
drwx------ 3 jnsahibz mc00 512 Mar 22 14:49 assignment
drwxr-xr-x 3 jnsahibz mc00 512 Mar 22 13:18 bin
-rw------- 1 jnsahibz mc00 2931431 Mar 19 17:21 junaid.zip
drwx------ 2 jnsahibz mc00 512 Mar 19 19:59 mail
drwxr-xr-x 4 jnsahibz mc00 512 Mar 21 19:41 public_html
drwx------ 3 jnsahibz mc00 512 Mar 13 18:45 zakir naik


now u see at the end of every line there is the name of the directory or file

that is what i need.

the reason i am using ls -l is because i wan to separate files and directories. so i can do that using "d" or "-" to make out whether it is a file or a directory.

papa ur code works for extracting directory names. how about extracting file names?
papa plz explain how to do the same for extracing the names of the files

this is what i am trying to do but unable to extract the file name

files=`ls -l |grep "^-" |cut -f20,25 -d" "`

Last edited by bahadur; 03-21-2005 at 11:16 PM.
 
Old 03-21-2005, 11:31 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
try find instead of ls
Code:
find . -type d -maxdepth 1 | cut -c 3-
 
Old 03-21-2005, 11:35 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
for directories:
Code:
ls -l | grep "^d" | gawk '{ print $9 }'
for files:
Code:
ls -l | grep "^-" | gawk '{ print $9 }'
 
Old 03-22-2005, 08:39 AM   #7
prasanta
Member
 
Registered: Mar 2005
Location: India
Distribution: Debian
Posts: 368

Rep: Reputation: 37
This will work for your directories

ls -l | tr -s " " | grep "^d" | cut -d " " -f 9

-Prasanta
 
Old 03-22-2005, 09:11 AM   #8
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
In my personal opinion (which is much like a butt, everyone has one), piping ls -l through grep, for this case, is kinda like buying a full nice suit just to get the tie.

To get files/dirs separately:
Code:
$searchdir="."
find "$searchdir" -type f -maxdepth 1 # files
find "$searchdir" -type d -maxdepth 1 # dirs
The beauty of find is that it provides a full path, relative to the search path. If that's not your bag, and all you need is the filename, then you can strip it using basename:
Code:
$searchdir="."
find "$searchdir" -type f -maxdepth 1 # files -exec basename '{}' ';'
find "$searchdir" -type d -maxdepth 1 # dirs -exec basename '{}' ';'
If any of the files or dirs will have spaces in the name, you'll need to adjust the value of IFS, to remove the space as a field separator. Here is another thread with information about using IFS
 
Old 03-22-2005, 09:37 AM   #9
innuendo_98
LQ Newbie
 
Registered: Mar 2005
Location: USA
Distribution: Mandrake
Posts: 19

Rep: Reputation: 0
Cool

This script might help you, what i do here is read the contents of an entire directory, and since i'll be working with the sub-dirs I read them into an array and from there I can open each dir ( array element ) and do all sorts of stuff. The code that I used is being used for backup purposes, where some of the structure is more all less the same and where there are differences in the tree structure I'd make some exceptions and work around it.


x=`find -maxdepth 1 -type d | sed -e '1d' sed 's/.\///g' 1>$DIR_LIST`

declare -a dirs
dirs=(`cat "$DIR_LIST"`)

cnt=0
numElem=${#dirs[*]}

while [ $cnt -lt $numElem ]
do
if [ ${dirs[$cnt]} == "clients" ]; then
echo "Working on Clients ... "

if [ -e $TARGET_IBOUND"/clients" ];then
cd ${dirs[$cnt]}
clients
cd .. #closes clients
else
mkdir -p $TARGET_IBOUND/${dirs[$cnt]}
cd ${dirs[$cnt]}
clients
cd ..
fi
fi

## Do whatever you want here

cnt=`expr $cnt + 1`

done
 
Old 03-22-2005, 07:48 PM   #10
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
thanks to all of those who took time to reply to me

the problem is that our teacher has clearly instructed us to keep away from the FIND command.

any one using this command will get a zero
 
  


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
CPIO will not extract File stress Linux - Software 2 07-22-2004 02:04 AM
how to extract .rar file treotan Linux - Newbie 2 07-16-2004 01:47 AM
how to extract file *.tgz vanhelsing Linux - Software 1 06-05-2004 07:07 AM
Extract file Electboy Linux - General 1 07-21-2003 06:51 PM
How do you extract a file? Neb Linux - Newbie 9 05-14-2003 04:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 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