Using ls on multiple directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using ls on multiple directories
# 1  
Old 08-02-2007
Question Using ls on multiple directories

Hi,

I am using ls command as below to get the list of *.edi files from 2 different subdirectories as1 & as2:

/ntekupal/test$ ls ./as1/*.edi ./as2/*.edi

It is giving me list of edi files in these folders as below :

./as1/6df667.edi ./as2/823ssd3.edi

Now my requirement is to just get the list of files without showing the absolute path like below :

6df667.edi 823ssd3.edi

Thanks
Raj.
# 2  
Old 08-02-2007
see if you systems supports basename

then you could pipe this to xargs basename to get what you want.
# 3  
Old 08-02-2007
I am new to unix so not sure about usage of xargs.
Can you please elaborate on that usage.

Thanks.
# 4  
Old 08-02-2007
Code:
ls ./as1/*.edi ./as2/*.edi | xargs basename

# 5  
Old 08-03-2007
Quote:
Originally Posted by ntekupal
Hi,

I am using ls command as below to get the list of *.edi files from 2 different subdirectories as1 & as2:

/ntekupal/test$ ls ./as1/*.edi ./as2/*.edi

It is giving me list of edi files in these folders as below :

./as1/6df667.edi ./as2/823ssd3.edi

Now my requirement is to just get the list of files without showing the absolute path like below :

6df667.edi 823ssd3.edi


Code:
printf "%s\n" ./as1/*.edi ./as2/*.edi | awk -F/ '{print $NF}'

Or:

Code:
cd as1 && printf '%s\n' *.edi
cd as2 && printf '%s\n' *.edi

# 6  
Old 08-03-2007
Using ls on multiple directories

Using xargs the way PORTER stated above will not work for both the files.

$ ls ./as1/*.edi ./as2/*.edi
./as1/jks.edi ./as2/rs.edi

$ ls ./as1/*.edi ./as2/*.edi | xargs basename
jks.edi


So this is the way :

$ ls -l ./as1/*.edi ./as2/*.edi | awk -F '/' '{print $NF'}
jks.edi
rs.edi

or

$ ls ./as1/*.edi ./as2/*.edi | while read file; do basename $file; done
jks.edi
rs.edi
# 7  
Old 08-03-2007
Quote:
Originally Posted by jaduks
$ ls -l ./as1/*.edi ./as2/*.edi | awk -F '/' '{print $NF'}

What's the point of using the -l option? You only want the filenames.

For that matter, why use ls at all?

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

RCP the multiple directories

Hi Friends, need to copy the folder from server1 to server2 below named folder with modification date server1 ---------------- Created date Name 19Aug folder1 (contain sub directories also) 20Aug folder2 (contain sub directories also) 21Aug folder2 (contain sub directories also) 21Aug... (2 Replies)
Discussion started by: rabindratech
2 Replies

2. Programming

RCP the multiple directories

Hi Friends, need to copy from server1 to server2 below name folder with modification date server1 ---------------- Created date Name 19Aug folder1 (contain sub directories also) 20Aug folder2 (contain sub directories also) 21Aug folder2 (contain sub... (0 Replies)
Discussion started by: rabindratech
0 Replies

3. UNIX for Dummies Questions & Answers

Deleting multiple directories inside multiple directories

Hi, Very unfamiliar with unix/linux stuff. Our admin is on vacation so, need help very quickly. I have directories (eg 40001, 40002, etc) that each have one subdirectory (01). Each subdir 01 has multiple subdirs (001, 002, 003, etc). They are same in each dir. I need to keep the top and... (7 Replies)
Discussion started by: kkouraus1
7 Replies

4. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

5. Shell Programming and Scripting

searching different multiple directories

Suppose I have 5 different directories in some path .Is it possible to search 5 different directories using find command in one single command line. I mean something like this:- find path -type d -name dirname1 dirname2 .......... pls help (2 Replies)
Discussion started by: maitree
2 Replies

6. Shell Programming and Scripting

FTP multiple files from multiple directories

I have multiple files that starts as TRADE_LOG spread across multiple folders in the given structure.. ./dir1/1/TRADE_LOG*.gz ./dir2/10/TRADE_LOG*.gz ./dir11/12/TRADE_LOG*.gz ./dir12/13/TRADE_LOG*.gz when I do ftp uisng mput from the "." dir I am getting the below given error mput... (1 Reply)
Discussion started by: prasperl
1 Replies

7. UNIX for Dummies Questions & Answers

Multiple search in multiple directories

Hi, I am facing the below problem I have a list of number to be searched(around 1000) files resideing in multiple directories as i dont know where it resides in. for example search string is like (abc)123456 please help. very urgent Thanks a lot Uma (3 Replies)
Discussion started by: umapearl
3 Replies

8. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

9. UNIX for Dummies Questions & Answers

Using ls on multiple directories

Hi, I am using ls command as below to get the list of *.edi files from 2 different subdirectories as1 & as2: /ntekupal/test$ ls ./as1/*.edi ./as2/*.edi It is giving me list of edi files in these folders as below : ./as1/6df667.edi ./as2/823ssd3.edi Now my requirement is to just get... (3 Replies)
Discussion started by: ntekupal
3 Replies

10. Shell Programming and Scripting

moving directories to new directories on multiple servers

Hi - I am new to unix scripts...I need to move several directories on multiple servers to new directories. (0 Replies)
Discussion started by: mackdaddy07
0 Replies
Login or Register to Ask a Question