LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2008, 09:42 AM   #1
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
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
 
Old 09-25-2008, 10:08 AM   #2
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
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.
 
Old 09-25-2008, 10:12 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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
 
Old 09-25-2008, 10:15 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by knockout_artist View Post
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?

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.
 
Old 09-25-2008, 10:45 AM   #5
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
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.
 
Old 09-25-2008, 11:04 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by knockout_artist View Post
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?

Last edited by i92guboj; 09-25-2008 at 11:07 AM.
 
Old 09-25-2008, 11:17 AM   #7
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by i92guboj View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change directory in a shell script? jdupre Linux - Newbie 13 06-25-2015 06:03 AM
Shell script help: How to pass a directory path to the script? dv502 Programming 13 09-10-2008 04:07 AM
shell script - process directory keith2045 Programming 7 05-31-2007 08:26 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration