shell script programming problem
hi guys,
i'm new to linux and i am facing a problem with filenames that have spaces in them. find $folder -type f -printf "%f/n" > file.tmp I use the above line to copy filenames in a folder to a temp file, but if the filename contain spaces it will be written as 2 separate lines. example: the filename "kenny rogers" will be displayed as kenny rogers what should i do to? thanks Ken |
Your command works as expected, but your script doesn't. We'll need to see the script to give an accurate solution.
|
Im trying to find files in a directory and sort them out according to highest frequency, then alphabetical order.
My code: #!/bin/sh start_folder="" if [ "$1"="" ]; then start_folder=. else start_folder=$1 fi find $start_folder -type f -printf "%f\n" > file1.tmp cat file1.tmp|uniq > file2.tmp for line in `cat file2.tmp` # problem starts here as the for loop treats the space as a delimiter do count=`grep -x $line file1.tmp|wc -l` echo $line $count >> file3.tmp done sort -k 2nr -k 1 file3.tmp | cut -d " " -f1 | head rm *.tmp |
Ok, I was worried you might be doing something non-standard. This is my normal solution to this kind of problem:
Code:
#!/bin/sh Also, the reason you need to surround $line in double quotes is for a similar reason. If your files contain spaces, grep can't know when your expression ends and filename(s) begin. |
Thanks a lot!!! Dark.
You're a life saver. By the way what commands can i use to test for empty files or directories? I'm still new to linux and not used to the syntax. |
The normal method is to use something like this:
Code:
if [ -e $some_filename ] ; then |
All times are GMT -5. The time now is 06:48 PM. |