How to get list of files only using ls without combining it with other command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get list of files only using ls without combining it with other command?
# 1  
Old 10-07-2013
How to get list of files only using ls without combining it with other command?

HI,

I have requirement to fetch the list of files except the ok file by connecting to other server and then copy all the files that are fetched using the below command.

Code:
ssh ${aSrcHOST} ls ${aSrcDIR}/grep -vi OK$ > filelist.txt

The above code is also picking up any directory names and dumping the same in filelist.dat. While I am trying to copy the files using the names present in filelist.dat, scp is failing for directory names.

Code:
for filename in `cat filelist.txt` ; do
scp -B -p ${Auser}@${aSrcHOST}:${aSrcDIR}/$filename $aTgtDIR
done

How can I pick up only file names and not the directory names using my first command.

Please help .
# 2  
Old 10-07-2013
Quote:
Originally Posted by Nikhath
I have requirement to fetch the list of files except the ok file by connecting to other server and then copy all the files that are fetched using the below command.
OK. But this code:
Code:
ssh ${aSrcHOST} ls ${aSrcDIR}/grep -vi OK$ > filelist.txt

has never run, will never run and definitely not do what you want.

First, ls ${aSrcDIR}/grep should probably read ls ${aSrcDIR}|grep, because otherwise you ask for a directory named "grep", which is located in "${aSrcDIR}" instead of filtering ls ${aSrcDIR} through grep.

Second, "OK$" will not work because "$" is special to the shell and need to be protected. In fact it might even need to be protected twice, first against the local system interpreting the commandline and second against the interpretation of the remote machine.

Finally, you called the file "ok" above and now you call it "OK". These are two different files, because UNIX is case-sensitive (in case you didn't know and it wasn't a simple typo).

At last, in the title you said you do not want to use any other command combined with ls, but in fact you use grep. This *is* an external command. Please, care to explain which it should be now?

If you want to use no other command you will have to use find instead of ls because the latter won't be able to do what you want on its own. If you want to allow grep you can use the "-F" switch of ls (see man ls for details) and use grep to filter the directories.


Quote:
Originally Posted by Nikhath
How can I pick up only file names and not the directory names using my first command.
Short answer: you can't, because your command is faulty, for reasons stated above. You either have to change your command or your expectations.

I hope this helps.

bakunin
# 3  
Old 10-07-2013
Thanks for the correction. The first code fragment contains a typo. It is actually a combination of ls and grep.

Code:
ssh ${aSrcHOST} ls ${aSrcDIR}/|grep -vi OK$ > filelist.txt

Here , I am trying to connect to aSrcHOST and fetch the list of files names present in aSrcDIR by filtering out ok/OK files. Hence using grep -vi

This is working fine and fetching all the file names except files ending with ok/OK and dumping the list of files names in filelist.txt.

But , this command is also picking up the sub directory names inside in aSrcDIR.
This is what I want to avoid. I want only files to be picked up and not the directory names.

Please help.
# 4  
Old 10-07-2013
ls cannot do this. Neither can grep magically tell apart directories and files without some systematic difference between names. You'll have to use a different way.

What's your system?
# 5  
Old 10-08-2013
Quote:
Originally Posted by Nikhath
But , this command is also picking up the sub directory names inside in aSrcDIR.
This is what I want to avoid. I want only files to be picked up and not the directory names.
I said it already above, but i will say it again: have a look at the "-F"-option of ls. Filenames will remain unchanged while directories will be added a "/" at their names:

Code:
# ls -1
testdir
testfile

# ls -1F
testdir/
testfile

This should be something you can easily grep for, no?

I hope this helps.

bakunin
# 6  
Old 10-08-2013
Hi,
And why do you not use 'find' ?
Code:
ssh ${aSrcHOST} "find ${aSrcDIR} -maxdepth 1 -type f -printf '%f\n'  > filelist.txt"

Regards.
# 7  
Old 10-08-2013
Almost nobody has -mindepth and -maxdepth unfortunately.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining 2 files

i am having 2 files like this file 1 1, 2, 3, 4, file2 5, 6, 7, 8, what i want do is like this i want to put all the contents for file 2 after file 1,means adding column in file1 (5 Replies)
Discussion started by: sagar_1986
5 Replies

2. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

3. Shell Programming and Scripting

combining 2 files into one according to the list

hi, i have a list of file: KB0002 KB215027 KB0003 KB215027 KB0004 KB215027 KB0005 KB204320 KB0006 KB207074 KB0007 KB215204 KB0008 KB223809 KB0009 KB236640 KB0010 KB244506 KB0011 KB205382 KB0012 KB212813 KB0013 KB236314 KB0014 KB230897 KB0015 KB232532 KB0016 KB231543... (2 Replies)
Discussion started by: karla
2 Replies

4. UNIX for Dummies Questions & Answers

combining two list according to one id..

Hi, I have two list I wanna combine and print them together in the output depending if they have the same rsid: file1: ENST00000262042 rs47 583 N D ENST00000408005 rs48 583 N D ENST00000311322 rs268 318 N S file 2: rs34 CHB T 1 C 0 T/T C/T C/C 1 0 0 rs47 JPT T 1 C 0 T/T C/T C/C 1 0 0 rs48... (6 Replies)
Discussion started by: karla
6 Replies

5. UNIX for Dummies Questions & Answers

combining two files

Hi Gurus, I have 2 files: File1 Filename1 xx Filename1 yy Filename1 Total Filename2 xx Filename2 yy Filename2 zz Filename2 Total Filename3 xx Filename3 Total and File2: Filename1 10296 xxx Date: 09/01/08 Filename2 10296 xxx Date: 09/05/08... (36 Replies)
Discussion started by: rock1
36 Replies

6. Shell Programming and Scripting

combining mv and compress command

Hi All, I have a file in DirA/ABC.out a need to move to DirB.I am using the following steps now: mv DirA/ABC.out DirB compress -f DirB/ABC.out Is there a way to just do this one step as I have to move hundreds of files every hour. -Thanks in advance. (4 Replies)
Discussion started by: sam_78_nyc
4 Replies

7. Shell Programming and Scripting

Combining two files

I have two files and I need to combine (not append - but combine a row to a row) eg: File1: apples grapes oranges lemons File2: red green orange yellow After combining, the file should look like: (the second column should start at a specific byte) apples red grapes green... (7 Replies)
Discussion started by: hemangjani
7 Replies

8. Shell Programming and Scripting

Combining Two Files

Could someone help me reduce the number of runs for a shell program I created? I have two text files below: $ more list1.txt 01 AAA 02 BBB 03 CCC 04 DDD $ more list2.txt 01 EEE 02 FFF 03 GGG I want to combine the lines with the same number to get the below: 01 AAA 01 EEE 02... (4 Replies)
Discussion started by: stevefox
4 Replies

9. UNIX for Dummies Questions & Answers

Combining files

Hi, is there a way to combine 2 files together, joining line 1 from file A with line 1 from file B, line 2 from A with line 2 from B etc. File A File B 1 4 2 5 3 6 Combined result = File C 14 25 36 (2 Replies)
Discussion started by: Enda Martin
2 Replies

10. UNIX for Dummies Questions & Answers

combining files

how will i combine these 2 files below, with the desired output specified below: file1: one two three four file2: red blue yellow green file3: aaa bbb ccc ddd (3 Replies)
Discussion started by: apalex
3 Replies
Login or Register to Ask a Question