LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-23-2016, 10:24 AM   #1
acacius
LQ Newbie
 
Registered: Jun 2015
Posts: 11

Rep: Reputation: Disabled
Bash script to rename files


I have bash 4.3.30. I am trying to make a script that will do the following:

If a filename in current directory contains a special character other than a dot or a dash, replace character with underscore.

Here is what I've tried:

Code:
#!/bin/bash

for FILE in *
do
 case [[ $FILE2 =~ (^\.a-zA-Z0-9\-) ]] in 

# trying to substitute a space here: but I get a
# syntax error at line 5.

  *\ *) $FILE2 = "${FILE2// /_}" ;;
 esac
  mv "$FILE" "$FILE2"
done
Can someone guide me?
 
Old 08-23-2016, 10:50 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
What is your strategy, replace a special specific character with a specific character in each case?
Wouldn't be more efficient to replace all special characters with a specific character in one pass?
 
Old 08-23-2016, 11:04 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,019

Rep: Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275Reputation: 1275
https://centos.tips/fixing-troublesome-filenames/
 
Old 08-23-2016, 11:55 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
substitution example
Code:
str=',;!!&@`(|^=+}'
echo ${str//[^\.a-zA-Z0-9\-]/_}

It's possible to limit the _'s, eg replace all consecutive special chars with one _
Code:
shopt -s extglob
str=',;!!&@`(|^=+}'
echo ${str//+([^\.a-zA-Z0-9\-])/_}
 
1 members found this post helpful.
Old 08-23-2016, 09:52 PM   #5
acacius
LQ Newbie
 
Registered: Jun 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TenTenths View Post
Great, that worked.
 
Old 08-24-2016, 03:13 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,570

Rep: Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335
Code:
  *\ *) $FILE2 = "${FILE2// /_}" ;;
is syntactically incorrect:
Code:
  *\ *) FILE2="${FILE2// /_}" ;;
#      ^    ^ ^  no $, no space before/after =
this is just one line, probably there are other errors
 
Old 08-24-2016, 08:56 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,358

Rep: Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971
Thanks keefaz! That is a keeper.
Code:
echo ${str//+([^\.a-zA-Z0-9\-])/_}
can also be written as
Code:
echo "${str//+([^[:alnum:].-])/_}"
 
1 members found this post helpful.
  


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 script to rename files elalexluna83 Programming 5 10-08-2013 01:31 AM
Help with bash script to rename multiple files zookman Programming 1 05-09-2011 04:23 AM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 04:50 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:22 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration