Moving files by splitting the path embedded in the filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Moving files by splitting the path embedded in the filename
# 1  
Lightbulb Moving files by splitting the path embedded in the filename

Hello All.

I am having a directory /tmp/rahul which contains many files in the format
@#home@#rahul@#programs@#script.pl
where /home/rahul/programs is the directory where the script.pl file is to be placed.

I have many files in this format.

What i want is a script which read these filenames from the /tmp/rahul directory and copy the files to their respective locations.
Actually i will run this script through a crontab for daily maintainence.

Thanks
# 2  
sounds like a good use for the case structure...

You can designate actions based on input parsed from your list. I am busy atm, but will post back or maybe someone can beat me to it.


case in {};

home)
cp file to dest;
;;

rahul)
cp file to dest;

;;

programs)
cp file to dest;

;;

script)
cp file to dest;

;;

*)
default if no match...
;;
esac
# 3  
wouldn't it be easier to simply substitute '@#' with '/' and copy the files?
# 4  
the "@#" in the filenames have to be replaced by "/" before the files can be moved/copied ...
Code:
#! /bin/ksh

for item in @#*
do
    a=`echo $item | sed -e "s/@#/\//g"`
    file=`basename $a`
    filedir=`dirname $a`
    if [ ! -d $filedir ]
    then
        mkdir -m 755 -p $filedir
    fi
    cp -p $item $filedir/$file
    ls -l $filedir/$file
done

exit 0

# 5  
Thnaks for ur Reply

Hi All,
Thanks for ur suggestions, I don't want to create directories as they already exist.
With sed i have found a very simple solutin as follows....

webpage_path='/tmp/customer_webpages'
for fileName in `ls -l $webpage_path|grep '^-'|tr -s ' ' ' '|cut -f9 -d ' '`
do
newPathAndFileName=`echo $fileName | sed -e "s/@#/\//g"`
cp $webpage_path/$fileName $newPathAndFileName
done

Tell me if i can optimise it better......Smilie
# 6  
the earlier code only checks to make sure that the directories exist and create them if they don't ... anyways, you already said that the filenames all start with "@#" so you only have to list those out ...
Code:
webpage_path='/tmp/customer_webpages'
for fileName in `cd $webpage_path; ls @#*`
do
    newPathAndFileName=`echo $fileName | sed -e "s/@#/\//g"`
    cp $webpage_path/$fileName $newPathAndFileName
done

# 7  
Quote:
Originally Posted by Just Ice
the earlier code only checks to make sure that the directories exist and create them if they don't ... anyways, you already said that the filenames all start with "@#" so you only have to list those out ...
Code:
webpage_path='/tmp/customer_webpages'
for fileName in `cd $webpage_path; ls @#*`
do
    newPathAndFileName=`echo $fileName | sed -e "s/@#/\//g"`
    cp $webpage_path/$fileName $newPathAndFileName
done

Does this script miss the destination directory ?
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #315
Difficulty: Easy
To bypass US Munitions Export Laws, the creator of the PGP published all the source code in a binary format.
True or False?

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. Shell Programming and Scripting

Moving files from parent path to multiple child path using bash in efficient way

Hi All, Can you please provide some pointers to move files from Base path to multiple paths in efficient way.Folder Structure is already created. /Path/AdminUser/User1/1111/Reports/aaa.txt to /Path/User1/1111/Reports/aaa.txt /Path/AdminUser/User1/2222/Reports/bbb.txt to... (6 Replies)
Discussion started by: karthikgv417
6 Replies

3. Shell Programming and Scripting

Non trivial file splitting, saving with variable filename

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have a few thousands text files (books). Each book has many chapters. Each chapter is identified by a cite-key. We need to split each of those book files by... (4 Replies)
Discussion started by: samask
4 Replies

4. Solaris

Moving a zone path to a new file system

Hello Gurus, I need your help with this please. I am trying to move a zone to a new directory but it keeps failing. Below is what I did and the message I get. Your help will be appreciated. Thanks zoneadm -z testzone move /rpool/testzone cannot create ZFS dataset rpool/testzone: dataset... (1 Reply)
Discussion started by: cjashu
1 Replies

5. Shell Programming and Scripting

How to add filepath to filename when moving it?

Hi I have a cron job setup that searches for 'core dump' files in a fixed set of directories and moves the core files to a separate file system. I am able to add date and time info to the file name. How can I add the source directory path to the file name? In case anyone is wondering why I... (5 Replies)
Discussion started by: dexter126
5 Replies

6. UNIX for Dummies Questions & Answers

finding and moving files based on the last three numerical characters in the filename

Hi, I have a series of files (upwards of 500) the filename format is as follows CC10-1234P1999.WGS84.p190, all in one directory. Now the last three numeric characters, in this case 999, can be anything from 001 to 999. I need to move some of them to a seperate directory, the ones I need to... (5 Replies)
Discussion started by: roche.j.mike
5 Replies

7. Shell Programming and Scripting

How to remove filename from the path

Hi, I have a list of paths with files at the end. How can strip off filenames. This is what I have: /apps/test/abc/file.txt /apps/new/home/daily/report.xml /apps/old/home/weekly/out/test.sh This is what I need: /apps/test/abc/ /apps/new/home/daily/ /apps/old/home/weekly/out/ ... (10 Replies)
Discussion started by: djanu
10 Replies

8. Shell Programming and Scripting

Problem with embedded FTP command in Ksh - System Cannot find the specified path.

I have the following FTP embedded in a Ksh script on AIX 5.3 ftp -n <<WHATEVER open 10.101.26.218 user hcistats ******* ascii put $thupdatefile put $thcollectfile quit WHATEVER Here is what my script returns: ... (3 Replies)
Discussion started by: troym72
3 Replies

9. Shell Programming and Scripting

Filename from splitting files to have the same filename of the original file with counter value

Hi all, I have a list of xml file. I need to split the files to a different files when see the <ko> tag. The list of filename are B20090908.1100-20090908.1200_CDMA=1,NO=2,SITE=3.xml B20090908.1200-20090908.1300_CDMA=1,NO=2,SITE=3.xml B20090908.1300-20090908.1400_CDMA=1,NO=2,SITE=3.xml ... (3 Replies)
Discussion started by: natalie23
3 Replies

10. Shell Programming and Scripting

moving files with spaces in filename from one directory to another

Hello, When I run following script #!/bin/bash cd ~/directory1 mv `ls -trF | grep -v / | tail -10 ` ~/directory2 works fine with filenames not having any space but runs into issues with filenames that have spaces tried with $file variable still doesnot work. Can someone help me (4 Replies)
Discussion started by: asakhare
4 Replies

Featured Tech Videos