LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2007, 12:52 PM   #1
Konane
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Rep: Reputation: 0
Bash Script newbie


I'm a newbie to bash scripting but I need this ASAP and don't have time to learn all the syntax.

I need a script to list files and their types in a given directory in a manner:

DIR folder1
DIR folder2
LINK data
FILE a.txt
FILE b.txt
...so on

Finnaly it gives the number of each
Directories = 2 , files = 2 , links = 1

I know it is basicly like ls, but I will surely overcomplicate things if I try to make it on my own.

The second part is to make a directory tree, given the maximum depth as a parameter:
f.e. depth 2:
l--DIR folder
l--DIR folder2
l--FILE a.txt
l--LINK data
l--FILE b.txt

Directories = 2, files = 2, links = 1

etc.

Help would be most welcome.
 
Old 11-17-2007, 01:11 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
The best advice would be for you to make time to learn the syntax instead of expecting us to do it for you "ASAP". It sounds like a homework question so I'd suggest you look at things like the man pages for stat, type and the advanced bash scripting guide.
 
Old 11-17-2007, 01:59 PM   #3
brazilnut
Member
 
Registered: Nov 2007
Posts: 113

Rep: Reputation: 16
That's class, and I thought(!) using slackware was a walk in the park! lol

Last edited by brazilnut; 11-17-2007 at 02:22 PM.
 
Old 11-17-2007, 02:42 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You posted a homework question in the worst possible way---it looks like you have simply copied the assignment sheet and there is no evidence of you having done any work.

What's worse, you say "you don't have time", but you somehow assume that we do........

You were already referred to the Advanced Bash Scripting Guide. I find it very useful to keep this on my system and use it for searches. Also at http://tldp.org you will find "Bash Guide for Beginners"

To the first question: I would think the "sort" command might be useful.

2nd question: Don't understand---do you mean create those file and directories? Look at the man pages for "mkdir", "touch", and "ln"
 
Old 11-18-2007, 04:35 AM   #5
Konane
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Hey I don't expect anyone to do it for me, I just want some quick tips which commands to use, because i don't have time to go through whole tutorials etc. Anyway, what would be the easiest way to save and then edit ls output, preferably without creating a file?

Thanks for answers.
 
Old 11-18-2007, 06:15 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Konane View Post
Hey I don't expect anyone to do it for me, I just want some quick tips which commands to use, because i don't have time to go through whole tutorials etc. Anyway, what would be the easiest way to save and then edit ls output, preferably without creating a file?

Thanks for answers.
You can save any data in one of two places: a file or in memory.
ls > filename (puts it in "filename")
varname=`ls` (puts it in a variable "varname")

I strongly suggest that you stop telling us that you "don't have time". In fact, your next question would best be in a new thread with a completely different tone to to it---and showing what you have tried, and where you are getting stuck.
 
Old 11-18-2007, 06:40 AM   #7
Konane
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Ok, so I want to edit the output like this

Folder1/ Folder2/
output.txt* script.sh* script.sh~*

into

Folder1 DIR
Folder2 DIR
output.txt FILE
script.sh FILE
script.sh FILE

or something similar. I've tried using sed command to substitute characters, but when I try to substitute / I get an error. And how do I put newline after each word(stupid, I know)?

Last edited by Konane; 11-18-2007 at 07:19 AM.
 
Old 11-18-2007, 11:18 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Konane View Post
Ok, so I want to edit the output like this

Folder1/ Folder2/
output.txt* script.sh* script.sh~*

into

Folder1 DIR
Folder2 DIR
output.txt FILE
script.sh FILE
script.sh FILE

or something similar. I've tried using sed command to substitute characters, but when I try to substitute / I get an error. And how do I put newline after each word(stupid, I know)?
If things are separated by spaces, and you want newlines, do this:

sed 's/ /\n/g' filename

If you want to replace the "/" character, you need to escape it, or use a different character for the sed delimiter.

Here is my favorite sed tutorial: http://www.grymoire.com/Unix/Sed.html
 
Old 11-18-2007, 11:25 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you also want to classify directory entries, it would be better to process the output of "ls -l". You can use either sed or awk to do the processing. AWK might be better because you can also tally the total space as well, or output all of the directory listings before all of the file listings.
Code:
ls -l /var
total 48
drwxr-xr-x  9 root  root  4096 2007-11-11 06:37 adm
drwxr-xr-x 18 root  root  4096 2007-11-11 09:16 cache
drwxrwxr-x  3 games games 4096 2007-11-11 06:03 games
drwxr-xr-x 39 root  root  4096 2007-11-18 07:02 lib
drwxrwxr-t  5 root  uucp  4096 2007-11-17 19:14 lock
drwxr-xr-x 10 root  root  4096 2007-11-18 06:56 log
lrwxrwxrwx  1 root  root    10 2007-11-11 05:48 mail -> spool/mail
drwxr-xr-x  2 root  root  4096 2007-09-21 14:18 opt
drwxr-xr-x 20 root  root  4096 2007-11-18 00:15 run
drwxr-xr-x 12 root  root  4096 2007-11-11 06:05 spool
drwxrwxrwt  5 root  root  4096 2007-11-18 10:48 tmp
drwxr-xr-x  3 root  root  4096 2007-11-11 06:01 X11R6
drwxr-xr-x  3 root  root  4096 2007-11-11 06:18 yp
The first letter indicates whether the entry is a file "-", a directory "d" or a link "l".

Most of the commands such as ls, mv, cp, ln, etal. are provided by the coreutils package. It's info manual can be helpful in learning these commands.
 
Old 11-18-2007, 12:46 PM   #10
Konane
LQ Newbie
 
Registered: Nov 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks very much, that helped a lot. Didn't notice that the first letter stands for the type
 
  


Reply



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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM
Bash script, read file into array (Newbie) TheMeeks Programming 6 04-17-2007 08:20 AM
Newbie bash question about a secondhand script madtinkerer Programming 22 11-27-2006 08:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:06 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
Open Source Consulting | Domain Registration