LinuxQuestions.org
Help answer threads with 0 replies.
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-20-2019, 01:18 PM   #1
kaplan71
Member
 
Registered: Nov 2003
Posts: 804

Rep: Reputation: 39
Scripting question


Hello --

I am writing a bash script that will copy a given list of folders in a source directory to a destination directory on an NFS mount.

Additionally, I want to include a conditional statement within the script that will check the destination directory for similarly named folders, and if it finds them, it will rename them with a date suffix. The goal being to avoid overwriting any folders at the destination.

The command that I have for renaming a given folder is the following:

Code:
mv $foldername foldername_$(date +%d%b%Y)
I don't what the correct syntax is for the conditional statement. Can someone provide information on this?

Thanks.
 
Old 09-20-2019, 01:31 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
personally I would use this date format

date +%F
2019-09-20

it "sorts" much easier that way

a simple if statement

Code:
#!/bin/bash

mydir="/path/to/somedir"

if [[ -e ${mydir} ]]
then
    echo "${mydir} exists"
else
    echo "${mydir} does not exist"
fi


if [[ -d ${mydir} ]]
then
    echo "${mydir} is a directory"
else
    echo "${mydir} is not directory"
fi

https://mywiki.wooledge.org/BashGuid...ndConditionals
 
1 members found this post helpful.
Old 09-20-2019, 01:59 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,133

Rep: Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925Reputation: 1925
if find like dir name then rename that directory then move/copy dir over onto ntfs?

Code:
source=
detestation=
#looks at entire string of destination
#to match the directory within  source
#if found rename destination then
#move or copy over the source onto ntfs
#using substring find

if [[ $detestation =~ ${source##*/} ]] ; then
   echo "  mv $detestation $detestation-$(date format to your liking) "
    echo " mv/cp $source $destination "
fi
echo to test first.

Last edited by BW-userx; 09-20-2019 at 03:32 PM.
 
Old 09-20-2019, 02:08 PM   #4
kaplan71
Member
 
Registered: Nov 2003
Posts: 804

Original Poster
Rep: Reputation: 39
Colleagues --

Thank-you both for your postings. I am going to be incorporating both suggestions into my script, and I will be testing it later today.

I will post the results when that is done.
 
Old 09-26-2019, 05:38 AM   #5
kaplan71
Member
 
Registered: Nov 2003
Posts: 804

Original Poster
Rep: Reputation: 39
Colleagues --

My apologies for the delay. The script has been completed, and its code, made generic, is the following:

Quote:
source=/<directory 1>
destination=/<nfs mount>
now=$(date +"%m_%d_%Y_%H_%M_%S")

for item in "$source"/*
do
echo "$item"
cp -r ${item} ${destination}"/"${item##*/}"-"${now}
done
Testing was successful. Thank-you both for the feedback.
 
Old 09-28-2019, 01:31 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,193

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
In command arguments put quotes around $variables, that avoids expansions on special characters.

Code:
cp -r "$item" "$destination/${item##*/}-$now"
As an alternative, you can cd to the source directory. Then you get the base file names.
Code:
cd "$source" || exit
for item in *
do
  echo "$source/$item"
  cp -r "$item" "$destination/$item-$now"
done
 
Old 09-28-2019, 08:56 AM   #7
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,771

Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
In command arguments put quotes around $variables, that avoids expansions on special characters.

Code:
cp -r "$item" "$destination/${item##*/}-$now"
As an alternative, you can cd to the source directory. Then you get the base file names.
Code:
cd "$source" || exit
for item in *
do
  echo "$source/$item"
  cp -r "$item" "$destination/$item-$now"
done
Just a small suggestion, but put '--' after 'cp -r'.

Code:
cp -r -- "$item" "$destination/$item-$now"
This indicates that there will be no more arguments and in the event "$item" expands to a variable starting with a '-' it won't be interpreted as an argument instead of a file.
 
3 members found this post helpful.
Old 09-28-2019, 09:45 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,193

Rep: Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547Reputation: 547
Good catch. To properly handle file names that begin with a - character.
-- is Posix.
The classic Unix workaround is to prefix a ./
Code:
cp -r "./$item" "$destination/$item-$now"
 
Old 09-28-2019, 10:00 AM   #9
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,771

Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
Good catch. To properly handle file names that begin with a - character.
-- is Posix.
The classic Unix workaround is to prefix a ./
Code:
cp -r "./$item" "$destination/$item-$now"
Its better to use '--' because prefixing a ./ to "$item" will still leave room for "$destination" expanding into an argument.

For example this works.
Code:
cp "./$item" "$destination/$item-$now" -r
While this won't and '-r' will be interpreted as a target directory.
Code:
cp -- "./$item" "$destination/$item-$now" -r
 
Old 09-28-2019, 08:56 PM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: CentOS, MacOS, [Open]SuSE, Raspian, Red Hat, Slackware, Solaris, Tru64
Posts: 1,503

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by Firerat View Post
personally I would use this date format

date +%F
2019-09-20
ISO-8601 for the win!

Lordy, I wish one could configure Linux to make this the default date/time format for everything ("ls", syslog, ... everything).
 
Old 09-28-2019, 09:24 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,320

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
yeah, I remember the first time I came across it on ftp
as soon as I saw the order of the files it was a light bulb moment

not only do you get sensible ordering, but you also don't need to figure out if it is mm-dd-yyyy or dd-mm-yyyy

I guess you could set alias

Code:
ls -l --time-style=long-iso
ls -l --time-style=+%F     
dmesg --time-format iso
 
Old 09-29-2019, 01:59 AM   #12
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: CentOS, MacOS, [Open]SuSE, Raspian, Red Hat, Slackware, Solaris, Tru64
Posts: 1,503

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by Firerat View Post
yeah, I remember the first time I came across it on ftp
as soon as I saw the order of the files it was a light bulb moment

not only do you get sensible ordering, but you also don't need to figure out if it is mm-dd-yyyy or dd-mm-yyyy

I guess you could set alias

Code:
ls -l --time-style=long-iso
ls -l --time-style=+%F     
dmesg --time-format iso
I've been using that format -- and been an evangelist for it -- for years. My converts number in the few, though. :^D

I longed for it a while back while helping to determine why some batch processes at a client site were aborting randomly each night---they'd been crashing for weeks before someone passed it off to me. I needed to merge a slew of job log files and determine what each job was doing at each point in time. The jobs were simply using "echo `date +%something` message" in their log entries (Multiple developers, multiple date formats. Great fun) and were useless to allow one to merge using "cat log1 log2 log3 ... logN | sort > combined.log". I had to run each log through a filter that recognized the various date formats and converted them to ISO format so the sorting didn't produce something completely useless. (Bottom line: Lazy database programmer's weren't checking whether there were any locks on a critical table and whatever batch job got to it first would work just fine but one or more of the other jobs would fail. Jobs tested just fine when run individually. No problem, right? So they then tell the scheduling team to run them concurrently. Hilarity ensues.)
 
Old 09-29-2019, 02:25 AM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: CentOS, MacOS, [Open]SuSE, Raspian, Red Hat, Slackware, Solaris, Tru64
Posts: 1,503

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by kaplan71 View Post
Hello --

I am writing a bash script that will copy a given list of folders in a source directory to a destination directory on an NFS mount.

Additionally, I want to include a conditional statement within the script that will check the destination directory for similarly named folders, and if it finds them, it will rename them with a date suffix. The goal being to avoid overwriting any folders at the destination.

<snip>

I don't what the correct syntax is for the conditional statement. Can someone provide information on this?
"man test" is your friend. You can test for everything that you might find in a filesystem, file, directories, symlinks, pipes, readability, writability, ... you name it. In general:
Code:
if [ <condition> <filesystem-object> ]; then
    <do-something>
fi
For example:
Code:
if [ ! -d target-tree/subdir ]; then
    cp -R subdir target-tree/
fi
Note: You might want to check to see if a regular file in the target tree exists with the same name as one of the subdirectories to be copied. You won't be able to overwrite it but checking for it lets avoid the error messages. Example:
Code:
$ mkdir info target
$ echo "data" > target/info
$ cp -R info target/info
cp: cannot overwrite non-directory 'target/info' with directory 'info'
You could^Wshould test for the presence of "info" being either a directory or a file before copying it. Either case would be, I think, a case for renaming the subdirectory as you're copying it.

HTH...


(Hmm... tried posting this earlier and, apparently, it fell victim to tonight's KDE crash. Sorry if this is no longer timely.)
 
Old 09-30-2019, 02:24 PM   #14
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 12,463
Blog Entries: 9

Rep: Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372Reputation: 3372
Quote:
Originally Posted by rnturn View Post
"man test" is your friend.
Isn't 'test' usually a shell builtin?
The man page is for the version provided by coreutils, but if you just use 'test' in a bash script, it would use the builtin.

Hence
Code:
help test
would be better.
 
  


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
[SOLVED] Crash Course in Python Scripting needed - helper must know python scripting and about audio files ending in m4a BW-userx Programming 4 04-07-2017 01:55 PM
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 04:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM

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

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