Help with move command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with move command
# 1  
Old 04-16-2012
Help with move command

I need help with a move command from one folder to another

In current folder there are files like test_1.txt, test_2.txt

I want to move this to another folder /archive with renaming files as
test_1_old.txt, test_2_old.txt (actually in old it should be a timestamp which I do it through etl tool)

I tried the below command and it is not working
Code:
mv /currentfolder/test_*.txt /archive/test_*_old.txt


Last edited by Franklin52; 04-17-2012 at 08:48 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 04-16-2012
It doesn't work that way.

The trick is, mv doesn't understand what * means. The shell itself substitutes * for you, before the command is run. So your command actually ends up being

Code:
mv /currentfolder/test_1.txt /currentfolder/test_2.txt /archive/test_*_old.txt

...with the last one remaining intact because no files were found with that name.

So it tries to move all those files into a folder named /archive/test_*_old.txt and complains because there's no such folder.

If you install the mmv command, it will work more like you expect, but you'll have to put the filenames in single quotes:

Code:
mmv '/currentfolder/test_*.txt' '/archive/test_#1_old.txt'

mmv is also a very safe command -- it will warn you when two files end up as the same name and refuse to do it, etc, etc.
# 3  
Old 04-16-2012
Hi,

Code:
for i in `ls /your_path/test*'
do
  new_file=`echo $i | cut -b1-6`
  mv $i /new_your_path/$new_file_old.txt 2> /path/error.log
done

Quim&r@

Moderator's Comments:
Mod Comment Code tags for code, please.
# 4  
Old 04-16-2012
That is a useless use of backticks and useless use of ls *. It literally serves no purpose because you only get from ls exactly what you put into it -- just * by itself will work fine in its place.

Code:
for i in /your_path/test*
do
...
done

# 5  
Old 04-16-2012
I will try the script. I thought I could do it using one move command Smilie
# 6  
Old 04-16-2012
mmv can, though it's not installed standard.
# 7  
Old 04-16-2012
What I gave is just an example but your script didn't work exactly what I need. The cut command doesn't work as the files are not of equal length

This is what I am exactly looking for

There are two files in one directory say

/project/files/ABC_Employee.txt
/project/files/ABC_user.txt

I want to move them to an archive folder with the current date at the end

/project/files/archive/ABC_Employee_20120416.txt
/project/files/archive/ABC_user_20120416.txt

Sorry for the trouble.

Thanks.

---------- Post updated at 04:45 PM ---------- Previous update was at 04:43 PM ----------

Corona688,

yeah and i do not have any admin privileges to install a command also the script may be executed in different servers. So, if I installed in dev and it will be a problem in prod, right?

If it cannot me done then the last thing to do is write two move commands Smilie

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Move and Rename in One Command

Hey all, I really need help with some homework I'm having on UNIX. This probably sounds stupid, but I'm being asked to move a file to a specific directory and rename it a specified name in one command. I know how to do it in more than one command, I just can't seem to figure it out using only one... (1 Reply)
Discussion started by: ayylmao12
1 Replies

2. Shell Programming and Scripting

Find and move command with exec

Hi all, I am trying to find files newer than a given file and them mv them to a new location. So I far I have: find . ! -newer <file_name> -exec ls -l {} \; and find . ! -newer <file_name> -exec mv /TEMP_LOCATION {} \; find is not liking this. Anyone know how to modify the last... (2 Replies)
Discussion started by: jonnyd
2 Replies

3. UNIX for Dummies Questions & Answers

Help required with move command

Hi I need to move the first file in a folder to an another folder but am facing issues with the below cmd. CAN someone correct me Ex : Folder :data/cat/tst ad2 ad4 ad5 ad6 output req: data/cat/man ad2 Command Used : ls -lt ad*|head -1|mv/data/cat/man (3 Replies)
Discussion started by: akshay01987
3 Replies

4. UNIX for Dummies Questions & Answers

Move command within script

Anyone have an idea to why the statement below does not work within a script but works when issued from the command line? mv /dir_files/submit.log* /bintemp (1 Reply)
Discussion started by: bwcberb
1 Replies

5. Shell Programming and Scripting

Bulk move in ssh command

Hi All, I am getting an problem. I am using ssh command in shell script to copy all files from a dir to another. find /home/dee -type f ! -newer /tmp/new | xargs -i cp {} /home/past{} This works fine but when I replace cp with mv, nothing happens! not even error. Can anybody please... (0 Replies)
Discussion started by: Deei
0 Replies

6. Shell Programming and Scripting

Question about move command

Is there a way to undo a move after running a command such as this: find ./* -type f -name "*.pdf" -exec mv {} /new/dir \; :confused: (3 Replies)
Discussion started by: bbbngowc
3 Replies

7. Shell Programming and Scripting

Need help on move (mv) command

Hi all, I am using c-shell ,Sun OS. When i am trying to move (mv) a file size more then 2 GB i am getting error like -- parameter value too high -- also ls -ltr command was not working on that . But when i move a file of size less then or just above 2gb ,It is working fine . Can... (2 Replies)
Discussion started by: jambesh
2 Replies

8. Solaris

move command

Hi, I am using SOLARIS 9. I am trying to move a huge number of files using the mv commands below- #mv inbuffer/* /tmp/inbuffer/ but system says arguments too long.. Could u please tell which command i need to put for moving AND copying ??? Many Thanks in advance-- Purple (3 Replies)
Discussion started by: thepurple
3 Replies

9. Shell Programming and Scripting

How to test whether a move command has been well executed

Hi its me again am having some prob to validate a script.. The script contain some mv file commands and i have to test whether the mv command has been executed successfully my move command are as follows......... mv -f... (6 Replies)
Discussion started by: Lutchumaya
6 Replies

10. UNIX for Dummies Questions & Answers

Help on MOVE command

Hi all, I'd like to use advanced features of mv command. Is it possible to move all files except one based on his extension ? I'm searching something like "mv *.* -*.xxx /dest_dir" ... Thanks all Michel. (2 Replies)
Discussion started by: michelr
2 Replies
Login or Register to Ask a Question