LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 08-21-2017, 01:39 AM   #1
1QAZ2WSX
LQ Newbie
 
Registered: Jul 2017
Posts: 9

Rep: Reputation: Disabled
Shell script for moving files into directory have common names


MY files are
1996-08-30T02.11.41.940_10
1997-03-04T17.24.17.650_17
1997-04-01T15.43.26.390_20

And folders
1996-08-30T02.11.41.940_5.1
1997-03-04T17.24.17.650_5.0
1997-04-01T15.43.26.390_4.5

the files and folders are in same directories. I want to move file to a folder whose name is matching
ex:
The file 1996-08-30T02.11.41.940_10 should be move in folder 1996-08-30T02.11.41.940_5.1



So far I have did, but no use

#!/bin/bash
for file in ./*T* ; do mv -v $file ${file%_*}; done

Last edited by 1QAZ2WSX; 08-21-2017 at 02:04 AM.
 
Old 08-21-2017, 01:54 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
I do not really understand. where are the files originally located, what is the target dir/filename, and what is the problem with your script?
 
1 members found this post helpful.
Old 08-21-2017, 02:06 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

I would probably do it with find, but as is your code is very close to working for the examples as given.

Code:
#!/bin/bash
for file in ./*T* ; do mv -v $file ${file%_*}; done
The red part does not make sense, but this should work:

Code:
#!/bin/bash
for file in ./*T* ; do mv -v $file ${file}_*; done
If the examples are truly representative of the actual files and directories, and the files are all in the parent directory of the targets where the script is run, that will be sufficient.

*** EDIT

pan64's post made me do a double-think and realise the above is not right...

So, falling back to find which I should have done first, and cleaning up after myself:

Code:
.
├── 1996-08-30T02.11.41.940
├── 1996-08-30T02.11.41.940_5.1
├── 1997-03-04T17.24.17.650
├── 1997-03-04T17.24.17.650_5.0
├── 1997-04-01T15.43.26.390
└── 1997-04-01T15.43.26.390_4.5

while read file; do
mv $file ${file}_*
done< <(find ./ -type f -name '*T*')

.
├── 1996-08-30T02.11.41.940_5.1
│   └── 1996-08-30T02.11.41.940
├── 1997-03-04T17.24.17.650_5.0
│   └── 1997-03-04T17.24.17.650
└── 1997-04-01T15.43.26.390_4.5
    └── 1997-04-01T15.43.26.390
That will still be fragile if you have filenames with spaces or special characters, but I leave that detail for an exercise!

Last edited by astrogeek; 08-21-2017 at 03:01 AM. Reason: Clean up my mess...
 
Old 08-22-2017, 06:09 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
With quoting of variables in conmand arguments
Code:
for file in ./*T* ; do mv -v "$file" "${file%_*}"_*/ ; done
The idea is that "${file%_*}" is the basename, and adding _*/ expands to all matching directories, where hopefully only one matches.

Last edited by MadeInGermany; 08-22-2017 at 06:35 AM. Reason: _ added
 
  


Reply



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] Creating folders and moving files in shell script Asoo Linux - Newbie 2 05-24-2017 07:48 AM
[SOLVED] move files or a directory to another directory using shell script user1111 Linux - Newbie 6 11-15-2013 08:13 AM
How to search for missing files and pass their names on to another shell script djslothario Linux - Newbie 3 08-07-2009 12:59 AM
shell script to move files from one system to another sytem with file names in a txt coral_km Linux - Newbie 3 02-13-2008 10:23 PM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 03:04 PM

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

All times are GMT -5. The time now is 11:13 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
Open Source Consulting | Domain Registration