Bourne shell viewing files in directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bourne shell viewing files in directory
# 1  
Old 05-13-2009
Bourne shell viewing files in directory

Hi,
I have a program that get a directory name from the user, then the program should go through one by one of the file, asking the user whether to move it to another folder. I tried to list the time of the file one by one. But it seems like it doesn't work. The code is as follow:

Code:
check()
{
     num=`ls -l $dir | wc -l '`
     for k in $num
     do
          time=`ls -l $dir | grep $9 | awk '{print $8} '`
          echo $time
          echo "Do you want to move this file to another folder?"
          ...
          ...
     done
}

It works fine when there is only one file in the folder. Somehow, when there are more than one files in the folder, they actually display the whole column of the time. Which means I can't take it one by one to display. Any help would be much appreciate.

Thank you.
# 2  
Old 05-13-2009
Is the single quote in this command a typo?

Code:
num=`ls -l $dir | wc -l '`

The for loop loops for each word separate by the field separator in the variable $num, that's not what you want. You should do something like:

Code:
i=0
while [ $i -lt $num ]
do
  # do your stuff here
  i=$(($i + 1))
done

# 3  
Old 05-13-2009
Oh yeah. I accidentally typed the single quote here. Sorry for that and thanks for the suggestion. I will try it right now.

Thank you very much.
# 4  
Old 05-13-2009
Hi,
That doesn't actually work for me. I tried to change the for loop to the while loop. The outcome is still the same.

Sample:
col1 col2 col3 col4 col5 col6 col7 col8 col9
f1 m1 i1 j1 k1 h1 g1 t1 u1
f2 m2 i2 j2 k2 h2 g2 t2 u2
f3 m3 i3 j3 k3 h3 g3 t3 u3

With the code above, it displays something like this:
t1 t2 t3

What I want is:
t1
"things that I want to do(cp, rm, or anything)"

t2
"things that i want to do(cp, rm, or anything)"

t3
"things that i want to do(cp, rm, or anything)"

Anyone has the idea how to do this?
# 5  
Old 05-13-2009
What is the content of $dir and is $9 the 9th parameter when you call the function in this command?

Code:
time=`ls -l $dir | grep $9 | awk '{print $8}`

# 6  
Old 05-13-2009
The $dir is actually the path for the directory that the user entered. The $9 is the file name as shown when we entered ls -l $dir. I was thinking that if I can search something like grep "hi" and get the files with the name hi, I thought I can do it this way as well.

I tried putting grep "$9". But it doesn't work as well. Hmmm
# 7  
Old 05-13-2009
That should not work and using grep with awk is redundant, why don't try something like this:

Code:
var="hi"
time=`ls -l $dir/$var | awk '{print $8}'`

instead of:

Code:
time=`ls -l $dir | grep $9 | awk '{print $8} '`

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

2. UNIX for Dummies Questions & Answers

Viewing file size in current directory

What is the command line for viewing the sizes(lines and bytes)of all the files in your present working directory? Is it >ls -la (2 Replies)
Discussion started by: Payton2704
2 Replies

3. Tips and Tutorials

Viewing changes in directory

Hi, I have a directory, and there is a job running and constantly writes and removes files from and to this directory. I would like to see somehow these changes without pressing `ls` every second. Kind of `tail -f` command, but for a directory list and not for file content. I thought maybe kind... (5 Replies)
Discussion started by: ilya_dv
5 Replies

4. Shell Programming and Scripting

List files in the current directory - BOURNE SHELL

i am trying to write a program, that will list .txt files and .png files. it will ask the user what type of files do they want to list! so if the user inputs txt files.. how would you list all the .txt files in the current directory (the directory the program is running)!! thanks (1 Reply)
Discussion started by: bshell_1214
1 Replies

5. UNIX for Advanced & Expert Users

HP-UX: Problem viewing directory with BDF...

Hi all, When I use BDF command on this particular server, it outputs mostly normal stuff. However, there is one directory it can't read at all. Also, it doesn't seem to exist. When I BDF my file system with a small panic script (it happens even if you use just the bdf command): As you... (17 Replies)
Discussion started by: zixzix01
17 Replies

6. Shell Programming and Scripting

How to activate Korn Shell functionnalities in Bourne Shell

Hi All I have writing a Korn Shell script to execute it on many of our servers. But some servers don't have Korn Shell installed, they use Borne Shell. Some operations like calculation don't work : cat ${file1} | tail -$((${num1}-${num2})) > ${file2} Is it possible to activate Korn Shell... (3 Replies)
Discussion started by: madmat
3 Replies

7. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

8. UNIX for Dummies Questions & Answers

Viewing Directory Content as You Navigate Directories in UNIX

Hi, Can someone help me figure out how to view directory content while I navigate directories (without having to go to the actual directory and "ls-ing" it)? Is there some keyboard shortcut for this? For instance, it would be useful if I could see the content of a directory when I'm copying... (2 Replies)
Discussion started by: shelata
2 Replies

9. UNIX for Dummies Questions & Answers

Viewing files

I have a file (called CORE) that is a dump created by a crashing process. This file, I believe, is in "binary" form, so when I try to use cat, more, or vi on it, it has a bunch of garbage. Is there anything I can use to "read" or view this file just like I might a non-binary file? I am running... (2 Replies)
Discussion started by: dsimpg1
2 Replies

10. UNIX for Dummies Questions & Answers

Viewing files in a directory?

How do I view files in a directory? (1 Reply)
Discussion started by: Ania
1 Replies
Login or Register to Ask a Question