LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-18-2005, 03:53 PM   #1
Transition
Member
 
Registered: Aug 2003
Distribution: Slackware 10
Posts: 48

Rep: Reputation: 15
Shell script to copy file name with part of directory


Here's the situation. I need to figure out who to take a file from a directory and automatically copy it as follows..

For example if i had a file in this directory

/usr/local/backups/db/01.18.05.Tuesday.gz

the desired result would be to copy this file into a new directory such as /copy with the name db.01.18.05.Tuesday.gz. So in addition to copying i need to strip off the directory name it's in and append it to the beginning of the file.



Any help is greatly appreciated.
 
Old 01-18-2005, 04:06 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
It ain't pretty, but:
Code:
echo "/usr/local/backups/db/01.18.05.Tuesday.gz" | sed 's@.*/\([^/]\+/[^/]\+\)@\1@' | sed 's@/@.@'
That turns the full path of the filename into the filename you wanted. Just run it and you'll see

There might be a much more elegant solution using basename and dirname, but I like sed, and the command above works (if a bit cryptic).

EDIT:
Actually, that command will break if the file is stored in the root (/) directory, or a relative directory with only one slash in the path. Just fyi.

Last edited by Dark_Helmet; 01-18-2005 at 04:08 PM.
 
Old 01-18-2005, 05:05 PM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
cp "$FILE" /copy/db.`basename "$FILE"`

should do the trick. e.g.:
cp /home/dave/hello.txt /copy/db.`basename /home/dave/hello.txt`

Dave
 
Old 01-18-2005, 05:14 PM   #4
Transition
Member
 
Registered: Aug 2003
Distribution: Slackware 10
Posts: 48

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by ilikejam
cp "$FILE" /copy/db.`basename "$FILE"`

should do the trick. e.g.:
cp /home/dave/hello.txt /copy/db.`basename /home/dave/hello.txt`

Dave
That doesnt append the directory name to the file though. I end up with a file called db.hello.txt instead of the desired dave.hello.txt.

Last edited by Transition; 01-18-2005 at 05:23 PM.
 
Old 01-18-2005, 05:33 PM   #5
Transition
Member
 
Registered: Aug 2003
Distribution: Slackware 10
Posts: 48

Original Poster
Rep: Reputation: 15
Got it.

cp /home/dave/hello.txt /copy/`basename /home/dave/`.hello.txt

Thanks ilikejam & DarkHelmet.
 
Old 01-18-2005, 05:40 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Code:
#!/bin/bash

FILE="</full/path/to/file.gz>"

DIRNAME=`dirname "$FILE"`
PREFIX=`basename "$DIRNAME"`
BASENAME=`basename "$FILE"`

cp "$FILE" /copy/"$PREFIX"."$BASENAME"
That should do it.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM
Making shell script a part of linux image mayureshchitale Linux - Newbie 1 07-06-2004 01:00 AM
Run script during file copy or create in directory neranjana Linux - General 1 01-13-2004 06:57 AM


All times are GMT -5. The time now is 09:28 AM.

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