LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-06-2015, 08:36 AM   #1
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Rep: Reputation: Disabled
Shell Script help. Don't know how to solve... Pls help


Hello

I have lot of textfiles written in vim editor. They are stored in multiple directories. Some have .txt extension and others have none. Reading them in linux is no problem. But when i try to open them in MS-Windows or smartphone, and because so many files have no .txt extension, it would ask to associate files with any program.

so i wrote a shell script to append .txt extension and also do some extra work for all files UNDER a directory.

The script is as follows:
Code:
#!/bin/bash
# Script Name: textren
# Renames text files.
# This shell does the following to files in a folder:
# 1) Check if the file is ASCII text.
# 2) Converts filename to lower case.
# 3) Replaces multiple spaces with single _.
# 4) Check if .txt extension exist, else append .txt extension.

for f in *; do
  # Check if 'f' is a file, and then check if it is a text file.
  if [[ -f "$f" && $(file "$f" | grep 'ASCII text$') ]]; then

    # Convert to lower case
    fLower="${f,,}";

    # Replace multiple spaces with single underscore
    fReplace="$(echo $fLower | sed 's/  */_/g')";

    # Find length of file
    length=${#fReplace};

    # Target file Add/Replace .txt extension
    if [[ "${fReplace:(( $length - 4 )):4}" != ".txt" ]]; then
      fTarget="$fReplace.txt";
    else
      fTarget="$fReplace";
    fi

  mv -nv "$f" "$fTarget" 2>/dev/null

  fi
done
OUTPUT: (Before):
Code:
$ ls -1
chrome plugins.txt
ffmpeg
Firefox-Plugins
git
mencoder.txt
nautilus DEL key
OPTIPING.TXT
pngcrush
thunderbird
Thunderbird 5 Shortcuts.pdf
tmux
OUTPUT: (After)
Code:
$ textren 
‘chrome plugins.txt’ -> ‘chrome_plugins.txt’
‘ffmpeg’ -> ‘ffmpeg.txt’
‘Firefox-Plugins’ -> ‘firefox-plugins.txt’
‘git’ -> ‘git.txt’
‘nautilus DEL key’ -> ‘nautilus_del_key.txt’
‘OPTIPING.TXT’ -> ‘optiping.txt’
‘pngcrush’ -> ‘pngcrush.txt’
‘thunderbird’ -> ‘thunderbird.txt’
‘tmux’ -> ‘tmux.txt’

$ ls -1
chrome_plugins.txt
ffmpeg.txt
firefox-plugins.txt
git.txt
mencoder.txt
nautilus_del_key.txt
optiping.txt
pngcrush.txt
Thunderbird 5 Shortcuts.pdf
thunderbird.txt
tmux.txt
So far so good. But now I realized, I have lot of sub-directories within sub-directories. It has become very painful to CD to every directory and run the script.

How do I make changes in the script so that it is applied recursively. I tried this - it doesn't work.

Code:
$ find . -type f -exec textren {} +
I'm scripting beginner and have written this script after a lot of research .

I just want to rename files recursively with this script. What do I have to change so that I can achieve that ?

Thank You.
 
Old 08-06-2015, 09:47 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,609

Rep: Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702Reputation: 702
Using find is a good idea. Remove your for loop and instead have your script take the filename as an argument:

Code:
f=$1
 
1 members found this post helpful.
Old 08-09-2015, 07:51 AM   #3
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 84

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by smallpond View Post
Using find is a good idea. Remove your for loop and instead have your script take the filename as an argument:

Code:
f=$1
OK thanks. I'll try.
 
  


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
Help to solve perl run shell script lamletoi Linux - Newbie 3 05-01-2012 08:56 AM
Pls help-shell script sameer9812 Linux - Newbie 2 06-27-2008 02:45 PM
Pls help-problem in shell script sameer9812 Linux - Newbie 3 06-26-2008 08:10 PM


All times are GMT -5. The time now is 02:13 AM.

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