Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ls -l | cut -f7 not working

Status
Not open for further replies.

sharma29

Technical User
Sep 11, 2003
10
0
0
GB
I am new to unix scripting, but I am trying to write something which can search a given directory pickup individual files for processing or passed into another script for doing some more work.

Stumbled in the first hurdle, cannot get the file names on its own
was trying ls -l | cut -f7

please help.
 
cannot get the file names on its own
ls

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry be more specific, trying to do something like,
hold the filenames one at a time and run through the loop doing few things.

ls -l | cut -f7
loop
sed 's/'&Civil Dev'/'and Rescue'/ file1
grep "New Account" >{file1}new.txt
grep -v "New Account" >(file1)old.txt
mv file1new.txt file1old.txt ./newdir
end loop

You can see a learner trying to figure out scripting.
 
a starting point:
Code:
ls | while read file
do "echo current file = '$file'"
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The find command can search a given directory for
files or folders etc.
man find for details.

Here find searches the current directory ( . ) for all files,
and print a line for each.
Code:
for i in `find . -type f`
do
  echo "Heres a file named $i "
done

or it can be piped to xargs for further processing by another command:
Code:
find . -type f | xargs ls -li

HTH
 
ls | while read fnm
do "echo current file = '$fnm'"
done

There must be something wrong tried using bash shell and ksh and both come back as
current file = '{}new.txt': not found

same for every other file in the dir. new.txt is a file in the the current dir.

SunOS 5.9 Generic_122300-08 sun4us sparc FJSV,GPUZC-M
 
this works ( that is ls minus one )

ls -1 | while read file
do echo "current file = $file"
done

current file = lsfiles
current file = test1.ksh
current file = log.out
 
Many thanks guys the
for i in `find . -type f`
do
..
done
works with a ./<filename>
But it is a good starting point for me. As I can use cut -c 3- to get rid of the ./
Thanks again. I will be definately using the forum again.
 
Using "basename" is better for getting rid of the path in a filename. Especially since "find" will go down into subdirectories, so that "cut -c 3-" won't actually get what you want. The "basename" command will.
Code:
#!/bin/ksh

echo "current file = $(basename $fnm)"

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top