LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 12-25-2006, 08:56 AM   #1
Katachi
LQ Newbie
 
Registered: Dec 2006
Posts: 3

Rep: Reputation: 0
Bash script: symbolic linking all files with same extension in a directory


First, this is my first bash script. Second off, it works. Third, its pretty pointless. Those caveats outta the way, my desire was to have a way to rename every file in a directory that was a certain extension, into files like <user selected string>1.extention. The directory where this takes place usually contains ~25 different files in the format of [UniqueUnintuitiveLabel]_Complicated_and_Long_Name_01_[38281809].foo or such. I just wanted something to instantly change all those ~25 filenames into something with a simpler name I could perform commands on, like a1.foo, a2.foo, a3.foo etc.

Anyways, this is the solution I came up with...

Code:
#!/bin/bash
echo "Enter the number of files:"
read foocount
echo "Enter an easy to type base name, such as an anacronym:"
read simple
echo "Enter the file extension(with period):"
read exten

#create a temporary file to contain all filenames
#of the user defined extention in the current directory
ls *"$exten" >> tempfile

#for each unmodified file name, create a symbolic link
#of the form <userdefinedname>1.
n=1
while [ $n -le $foocount ]
do
    filenamen=$(sed -n "$n{p;q;}" "tempfile")
    ln -s $filenamen $simple$n
    n=$((n+1))
done
rm tempfile
Is there a simpler/more elegant way to accomplish this? In particular, I have a feeling theres a way to get around the need for a stdout filled tempfile.

Symbolic links were used instead of "mv" because I need the original files to keep their names.

I know that TAB completion makes this script pretty useless, I really just wanted to learn some bash scripting.

Thanks.
 
Old 12-25-2006, 09:42 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Since the extension doesn't change, you could use file globing instead of manually counting the number of files that contain the extension:

Code:
#! /bin/bash
basename=$1; ext=$2
count=1
if [ "${#*}" != 2 ]; then cat << EOF
Usage: $0 basename extension
EOF
fi

for file in *.$ext; do
  ln -s "${file}" "$basename-$count"
  count=$(( ${count}+1 ))
done
I hope I didn't misunderstand your outcome.
This version takes the simple basename of the link and the extension from the parameters of the command.
The << EOF
...
EOF
part is a common technique. Instead of producing a bunch of echo statements, simply type in the message. Notice that $0 is expanded in the script. You can use this same technique to send emails to a group of other users, expanding some items read in to personalize the email.

I didn't comment my example as well as you did yours.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to access all files in a directory shinni Programming 5 04-24-2009 03:46 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM
bash script to sort files by extension otheralex Programming 7 08-19-2005 02:40 AM
bash script to set extension association vi0lat0r Programming 6 05-24-2004 07:04 PM


All times are GMT -5. The time now is 07:04 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