Operating System - HP-UX
1843341 Members
5108 Online
110214 Solutions
New Discussion

Creating files from ls -l list

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Creating files from ls -l list

HI

I have the following script for getting the list of subfilesystems under a particular filesystem. The list of parent filesystems are in the file CPUlist.

#cat CPUlist
/fs12
/fs13
/fs14
/fs15
/fs16
/fs19
/fs23
/fs31
/fs34
/fs36
/fs37
/stor

#cat getFS.sh
#!/bin/sh
for i in `cat CPUlist`
do
ls -l $i > /tmp/pgfs1/SYNC/i.txt
done

Whenever the shell tries to create a file under /tmp/pgfs1/SYNC/, it would produce the following error:

A file or a directory in the path does not exist:
./getFS.sh[5]: /tmp/pgfs1/SYNC/i.txt : 0403-005
cannot create the specified file.

Could someone help out in this question?

I would like to have the names of the files created under /tmp/pgfs1/SYNC/ to bear the names of the all filesystem list in CPUlist i.e. fs37.txt, fs36.txt, etc... fs12.txt.

Thanks.


5 REPLIES 5
Peter Kloetgen
Esteemed Contributor

Re: Creating files from ls -l list

Hi Chern,

what you did in your script is to redirect the output of your ls- command into a file. To get all entries of your listing into the file, use the following syntax:

ls -l Si >> /tmp/pgfs1/SYNC/i.txt

otherwhise the entries will be overwritten. To create files without contenst, you can use the touch- command, and if you want to add .txt to the name, use the following command in your loop:

touch "$i.txt"

If you need a path, put it in front of your desired name.

touch "/tmp/pgfs1/SYNC/$i.txt"

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Justo Exposito
Esteemed Contributor

Re: Creating files from ls -l list

Hi,
Exist the directory?
if not you can must create it before.

You must use >> if you don't want to overwrite the contents of the file.

Regards,

Justo.
Help is a Beatiful word
Chern Jian Leaw
Regular Advisor

Re: Creating files from ls -l list

Justo, Peter,

I had only wanted have the contents(ls -l) of the filesystem names as listed in file CPUlist to be redirected into another file.

I made changes to the line begining with ls -l ...
to become:

ls -l $i > /tmp/pgfs1/SYNC/.$i.txt

OR

I also tried removing the slashes '/' from the CPUlist file, and changed the line below in my script:
ls -l /$i > /tmp/pgfs1/SYNC/$i.txt

Neither both ways worked.

Any ideas?
Steve Steel
Honored Contributor
Solution

Re: Creating files from ls -l list

Hi
try
#!/bin/sh
cat getFS.sh|while read dir
do
ls -l $dir > /tmp/pgfs1/SYNC/$dir".txt"
done


Also check that each part of the directory
path till SYNC exists and can be written by
the user running the script.



Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Justo Exposito
Esteemed Contributor

Re: Creating files from ls -l list

Hi Chern,

Try protecting the variables:

ls -l /${i} > /tmp/pgfs1/SYNC/${i}.txt


Regards,

Justo.

Note: Do you have enough permits on the directory?

Help is a Beatiful word