ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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"
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?
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.
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)?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.