LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-13-2005, 06:39 AM   #1
sqn
Member
 
Registered: Apr 2002
Distribution: Ubuntu, Arch, freeBSD, Slackware
Posts: 210

Rep: Reputation: 30
shell scripting full directory listing


hi all,

I have to write an shell script that starts from a directory that was given as a parameter, and makes a list of all names (files and folders) that appear, modifying their names by adding a number that sad how many times the file/folder appears
ex:

file2 - meas that this file appears 2 times
folder4 - means this folder appears 4 times




And i have no ideea on how to start it

thx in advanced
 
Old 04-13-2005, 08:57 AM   #2
tangle
Senior Member
 
Registered: Apr 2002
Location: Smithville, TN
Distribution: Slackware
Posts: 1,744

Rep: Reputation: 71
I am not a bash scripting expert or for that matter a novice. But the ls -R / will list all directories and their sub diresctories.

You would need to take the out put of that and match the findings. There are a few tutorials on www.tldp.org. You might want to read them. There is a beginning and an advanced guide.
 
Old 04-13-2005, 09:35 AM   #3
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
Basically what you have to do is to find a way to traverse through all subdirectories and collect the dir and file name along the way. If a name already seen before, a counter associated with that name should be incremented.

Well, here is how it can be done:

Code:
#!/bin/bash

declare -a fns
declare -a fns_cnt
declare -a dirs
declare -a dirs_cnt

if [ "$1" == "" ]
then
    target="."
else
    target=$1
fi

for f in $(find $target)
do
    if [ -f $f ]
    then
        # echo "File $f"
        b=`basename $f`
        i=0
        find=0
        # check if file exists
        while [[ $i -lt `expr ${#fns[@]} - 1` ]]
        do
            if [ "x${fns[$i]}" == "x" ]
            then
                let "i += 1"
            else
                find=1
                break
            fi
        done
        if [ "$find" == "1"  ]
        then
            fns_cnt[$i]=$(( ${fns_cnt[$i]} + 1 ))
        else
            dim=${#fns[@]}
            fns[$dim]=$b
            fns_cnt[$dim]=1
        fi
     fi
done

echo ${fns[*]}
echo ${fns_cnt[*]}
OK,
1) it only checks file (not dir. But you can expand it very easily if you understand it)
2) the print out is not exactly in your stated format. But it is a very easy to adopt
3) command 'find' is expected to be available

Last edited by ahwkong; 04-13-2005 at 09:37 AM.
 
Old 04-13-2005, 11:33 AM   #4
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Another method would be:-
Code:
ls -lC1R | sed -e '/.*:\|^$/d' | sort | uniq -c
This will provide the information you require in this format:-
Code:
          
      2 dir_a
      4 dir_b
      2 dir_c
      1 dir_d
      1 dir_f
      2 file_a
      3 file_b
      1 file_c
where the first field is the number of occurences and the second field is the file/dir name.

It will not differentiate between files and directories, so if you want to do this check out "man ls" for file listing options. This is probably a good idea, otherwise if you have a file called "dog" and a directory called "dog" it will count them both in the same line.

When you format the output I would suggest putting something between the file name and the number of occurences, otherwise if you have a listing with "pic123", for example, how would you know if it is 3 x "pic12" or 23 x "pic1" or even 123 x "pic"?
 
Old 04-13-2005, 04:33 PM   #5
ahwkong
Member
 
Registered: Aug 2004
Location: Australia
Distribution: Fedora
Posts: 282

Rep: Reputation: 30
I love oneliner! :-)

sqn, ahh's is the right way to do it.

Last edited by ahwkong; 04-13-2005 at 04:35 PM.
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
Using 'ls' to get full path listing for mp3gain sygin Linux - Software 4 09-06-2005 08:55 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
Does anyone know how to move files to a particular directory using shell scripting RowanB Programming 3 11-11-2004 01:46 PM
recursively listing directories full paths kubicon Linux - Newbie 2 02-22-2004 03:55 PM


All times are GMT -5. The time now is 09:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration