LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-01-2013, 08:46 PM   #1
vk4led
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Bash Script for eenaming Multiple files


G'day

I am trying to get a script working that will copy existing files (most do no have an extension) to another folder and append all files with the extension .txt

I have the cp command down pat (cp /existing_folder /new_folder)

however I am stuck on the rename part. I have had some help with suggestions in getting some scripting down in order to rename files that dont have extension and move them to another folder (code below)

Code:
SOURCE_DIR="/home/webstuff"
TARGET_DIR="/home/webstuff/trial"

for SF in $SOURCE_DIR/*
do
  case `basename "$SF"` in
    *.* )  
      # Has an extension
      TF=$SF
      ;;
  * )
      # Does not have an extension
      TF="$SF.txt"
      ;;
  esac

  cp $SOURCE_DIR/$SF $TARGET_DIR/$TF
  if [[ $? -eq 0 ]]; then
    echo "Successfully copied $SOURCE_DIR/$SF to $TARGET_DIR/$TF"
  else
    echo "$? : Error occured coping $SOURCE_DIR/$SF to $TARGET_DIR/$TF"
  fi
done
however it is spitting out the following:

Quote:
cp: cannot stat `/home/webstuff//home/webstuff/setMonDate': No such file or directory

1 : Error occured coping /home/webstuff//home/webstuff/weboutput to /home/webstuff/trial//home/webstuff/weboutput.txt

To me this is a problem with the copying of the files? Maybe a double copy??

The files do exist in the SOURCE_DIR="/home/irlp/webstuff" dir

I am seeking suggestions in getting the bash script working (Debian System)

Most files do not have an extension (they are scripts) and some do (eg .conf)

I am hoping to copy the files to a specific folder, append them with .txt and make them viewable for other on the web.

Hope that makes sense and I appreciate your input
 
Old 09-01-2013, 10:23 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

I can see two problems:

1. When setting the TF you need to strip the $SOURCE_DIR path from $SF.
Use the following (assuming bash) to set TF:
Code:
TF="${SF##*/}"
or if adding .txt
Code:
TF="${SF##*/}.txt"
2. When calling cp you should be using just $SF instead of $SOURCE_DIR/$SF
Use the following

Code:
cp "$SF" "$TARGET_DIR/$TF"
Also, it is best to quote your variables (eg "$SF" instead of $SF)... consider what might happen if your filenames contained spaces.

HTH,

Evo2.
 
Old 09-01-2013, 10:28 PM   #3
vk4led
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Many thanks for the reply

I will pop the amendments into the script soon and give it a run

I will report back on the findings

Thanks again
 
Old 09-01-2013, 11:55 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

I just noticed that you are using the basename command. The ${SF##*/} does basically the same thing but without the need to call and external executable.

Cheers,

Evo2.
 
Old 09-02-2013, 04:39 AM   #5
vk4led
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Many thanks for your help. The script now works well.

Appreciate the input and assistance
 
Old 09-02-2013, 06:53 AM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
PS. To get into good habits, the for line should looks as follows:
Code:
for SF in "$SOURCE_DIR"/*
 
Old 09-02-2013, 10:18 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
There's no need to call basename too:

Code:
#!/bin/bash

SOURCE_DIR="/home/webstuff"
TARGET_DIR="/home/webstuff/trial"

for SF in "$SOURCE_DIR"/*; do
  TF=${SF##*/}
  [[ $TF != *.* ]] && TF=$TF.txt
  TF=$TARGET_DIR/$TF
  cp "$SF" "$TF"
  if [[ $? -eq 0 ]]; then
    echo "Successfully copied $SF to $TF."
  else
    echo "$? : Error occurred copying $SF to $TF."
  fi
done

Last edited by konsolebox; 09-02-2013 at 10:20 AM.
 
Old 09-02-2013, 07:40 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You could shorten even a little further as the use of $? is not required when 'if' can already test the return code:
Code:
if cp "$SF" "$TF"; then
Using konsolebox's code as example.
 
1 members found this post helpful.
Old 09-02-2013, 09:45 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Alternate
http://www.tldp.org/LDP/abs/html/lis...ml#LISTCONSREF
Code:
cp "$SF" "$TF" && echo "Successfully copied $SF to $TF." \
               || echo "$? : Error occurred copying $SF to $TF." >&2
note the >&2, it is sending output to stderr.. just for fun
 
  


Reply



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 with bash script to rename multiple files zookman Programming 1 05-09-2011 03:23 AM
[SOLVED] Bash script to extract multiple files with password musonio Linux - Software 8 03-12-2011 04:11 AM
Help with Bash Script - Rename Multiple Files embsupafly Programming 16 04-02-2010 03:50 AM
bash scripting: sorting and using using multiple files in a script daberkow Linux - Newbie 8 05-28-2009 09:24 AM
bash script: open multiple rar files chief_officer Programming 1 11-16-2007 02:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 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
Open Source Consulting | Domain Registration