LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Walking through directory Shell script (https://www.linuxquestions.org/questions/linux-newbie-8/walking-through-directory-shell-script-672292/)

knockout_artist 09-25-2008 08:42 AM

Walking through directory Shell script
 
Good Day,

How do we go into directory and read every thing in it.

Just up to one level down.

As
Code:

go into dir
if file , then pritn its a file
if directory then print its a directory.

Please note I can't use find command for that.
It has to be a step by step script.
Like read entry decide some thing and then move to next.

Thanks

unihiekka 09-25-2008 09:08 AM

If you want to go into a directory /home/username/something/ just type

Code:

cd /home/username/something
If you then want to see what's in it type

Code:

ls
or append the "-l" (list) or "-a" (all) parameter. So, for example,

Code:

ls -a
and you'll get all files and folders in the directory after you have hit Enter. It's really basic, you should be able to google it!

In list mode (ls -l) you see whether it is a directory (d) or a file (-) in the first entry.

ilikejam 09-25-2008 09:12 AM

Hi.

The first column of the 'ls -l' output tells you if each thing is a file, directory, device etc, so you can grep for '^d' to find directories, '^-' for files.

Alternatively, you could just run 'file *'.

Dave

i92guboj 09-25-2008 09:15 AM

Quote:

Originally Posted by knockout_artist (Post 3291356)
Good Day,

How do we go into directory and read every thing in it.

Just up to one level down.

As
Code:

go into dir
if file , then pritn its a file
if directory then print its a directory.

Please note I can't use find command for that.
It has to be a step by step script.
Like read entry decide some thing and then move to next.

Thanks

Well, did you assume you can't use find or is it that your teacher forbids it? :p

Code:

find . -maxdepth 2 | while read file
do
  #if $file is a file, do something
  #if $file is a dir, then do something else
done

Yes, you could use a recursive approach, but that would add complexity to the script, and wouldn't report any benefit.

knockout_artist 09-25-2008 09:45 AM

Thanks for every one's input.

But I need to hold ever thing in an array or some thing.

my script should do this
Code:

go in the directory
look around
check first entry , if file: copy to a certain location --> /file  copy to /new/file
check next entry,  if not file then copy ---> /dir/file-name copy to /new/dir/file-name
and do same with every entry.


i92guboj 09-25-2008 10:04 AM

Quote:

Originally Posted by knockout_artist (Post 3291437)
Thanks for every one's input.

But I need to hold ever thing in an array or some thing.

my script should do this
Code:

go in the directory
look around
check first entry , if file: copy to a certain location --> /file  copy to /new/file
check next entry,  if not file then copy ---> /dir/file-name copy to /new/dir/file-name
and do same with every entry.


Again, my solution with find do just that. Instead of overlooking it, let us know *why* exactly you don't want to use find.

EDIT: Bash is not really suited for arrays of a random unknown length. And, why would you need an array if you just can process the entries on the fly?

knockout_artist 09-25-2008 10:17 AM

Quote:

Originally Posted by i92guboj (Post 3291454)
Again, my solution with find do just that. Instead of overlooking it, let us know *why* exactly you don't want to use find.

EDIT: Bash is not really suited for arrays of a random unknown length. And, why would you need an array if you just can process the entries on the fly?


Thanks!

I kind of figured it out that I need to use php to do What I want to do.
Its just system updating script.
in one directory I have updated file and I will be copying them to related directory.


All times are GMT -5. The time now is 05:04 PM.