LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 08-25-2004, 10:30 AM   #1
ridertech
Member
 
Registered: Dec 2003
Location: Seattle, Washington
Distribution: Debian 'Sarge'
Posts: 85

Rep: Reputation: 15
Shell scripting to find length of filenames


I'm trying to copy files from one hard drive to another. The problem is that the destination drive does not accept files over 30 characters (very weird!). I already hava a script (below) that recursively searches the current directory and outputs the total number of files in each folder.

How can I recursively search through the current directory and output the complete path to only filenames that are over 30 characters? I assume it would be a simple modification to the script below, but my shell scripting is still limited...

Code:
#!/bin/sh

IFS='
'
for i in `find . -type d`; do
    DIRNAME=`echo $i | sed "s/'/\'/g"`
    echo "Directory $i has [`ls $DIRNAME | wc -l`] files"
done
 
Old 08-25-2004, 12:01 PM   #2
ridertech
Member
 
Registered: Dec 2003
Location: Seattle, Washington
Distribution: Debian 'Sarge'
Posts: 85

Original Poster
Rep: Reputation: 15
Sorry to post twice, but here is the solution is below...

Code:
#!/bin/sh

IFS='
'
for i in `find . -type d`; do
    DIRNAME=`echo $i | sed "s/'/\'/g"`
    
    for f in `ls $DIRNAME`; do
        LENGTH=`echo $f | wc -m`
        #echo "[$LENGTH] $DIRNAME/$f"
        
        if [ $LENGTH -gt 31 ]; then
            echo "[$LENGTH] $DIRNAME/$f"
        fi
    done
done
Now I just need to figure out how to cut-down the long filenames. I know moving it to a new name is the process, but I'm not sure how to get only the first 26 characters of the long name.

For example...
"long-ass-file-names-are-not-my-friend.mp3" would be moved to "long-ass-file-names-are-no.mp3"
 
Old 08-25-2004, 12:07 PM   #3
Demonbane
Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
you can pipe it to "cut"
Code:
echo "long-ass-file-names-are-not-my-friend.mp3" | cut -c 1-26
 
  


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
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM
bash scripting: problem with generating filenames mad_juno Programming 2 07-22-2005 10:57 AM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Length of filenames supreme_command Linux - Newbie 1 05-06-2004 09:15 PM
Where to find about shell scripting in the internet green_one Linux - Software 3 04-23-2004 05:33 PM


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

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