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. :confused: Any help is greatly appreciated. |
It ain't pretty, but:
Code:
echo "/usr/local/backups/db/01.18.05.Tuesday.gz" | sed 's@.*/\([^/]\+/[^/]\+\)@\1@' | sed 's@/@.@' 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. |
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 |
Quote:
|
Got it.
cp /home/dave/hello.txt /copy/`basename /home/dave/`.hello.txt Thanks ilikejam & DarkHelmet. :) |
Code:
#!/bin/bash |
All times are GMT -5. The time now is 04:28 PM. |