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.

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.

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 .

OK. But this 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.

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

Thanks for the correction. The first code fragment contains a typo. It is actually a combination of ls and grep.

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.

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?

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:

# ls -1
testdir
testfile

# ls -1F
testdir/
testfile

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

I hope this helps.

bakunin

Hi,
And why do you not use 'find' ?

ssh ${aSrcHOST} "find ${aSrcDIR} -maxdepth 1 -type f -printf '%f\n'  > filelist.txt"

Regards.

Almost nobody has -mindepth and -maxdepth unfortunately.

Another way (no -maxdepth and no -printf):

ssh ${aSrcHOST} "find ${aSrcDIR}/* -prune -type f -exec basename  {} \; >filelist.txt"

Regards.