Moving alphanumeric files according to the digit in file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving alphanumeric files according to the digit in file name
# 1  
Old 11-23-2017
Moving alphanumeric files according to the digit in file name

This is the content of my directory

Code:
c_g_se1_gb.ph
c_g_se1_gb.ph_pl_s.t
c_g_se1_gb.ph_pl_tr.t
c_g_se3_gb.ph
c_g_se3_gb.ph_pl_s.t
c_g_se3_gb.ph_pl_tr.t
c_g_se2_gb.ph
c_g_se2_gb.ph_pl_s.t
c_g_se2_gb.ph_pl_tr.t
c_g_se4_gb-1.ph
c_g_se4_gb-1.ph_pl_s.t
c_g_se4_gb-1.ph_pl_tr.t
c_g_se4_gb-2.ph
c_g_se4_gb-2.ph_pl_s.t
c_g_se4_gb-2.ph_pl_tr.t

I want to create new directories and simultaneously move the files in the corresponding directories according to the first digit in the file name which is attached with the c_g_se segment. In this case there will be four directories like New_dir1, New_dir2 .. so on. Here c_g_se1_gb.ph, c_g_se1_gb.ph_pl_s.t and c_g_se1_gb.ph_pl_tr.t will be the content of New_dir1.
# 2  
Old 11-24-2017
Any attempts / ideas / thoughts from your side?
# 3  
Old 11-24-2017
Quote:
Originally Posted by RudiC
Any attempts / ideas / thoughts from your side?
This is what I did for testing but its not creating multiple directories. If it work then I will add may be two more for loops for .ph_pl_s.t and .ph_pl_tr.t files

Code:
#!/bin/bash
if [ "$1" == "-h" ]; then
  echo "Usage: `basename $0` [Please Enter the starting_file_digit and ending file digit: run as find_move.sh 1 3175]"
  exit 0
fi
printf "\n"
echo Finding and moving *.ph Files from c_g_se"$1"_gb.ph to c_g_se"$2"_gb.ph
for (( c="$1"; c<="$2"; c++ ))
do  
   find -name '*.ph' -exec sh -c 'mkdir -p New_dir"$c" && cp "$@" New_dir"$c"' _ {} +
   printf "\n"
done

# 4  
Old 11-24-2017
Do you want to mv or to cp the files?
And, unlike what you showed in post#1, the number can be multi-digit?
# 5  
Old 11-24-2017
Quote:
Originally Posted by RudiC
Do you want to mv or to cp the files?
And, unlike what you showed in post#1, the number can be multi-digit?
I want mv for for testing I was using cp. Yes the number can be multi-digit. So I used loop here.
# 6  
Old 11-24-2017
Yes, you need a loop. How about
Code:
for FN in *.ph*; do TMP=${FN%_${FN#*_*_*_}}; NDTMP="NewDir${TMP#${TMP%%[0-9]*}}"; echo mkdir -p "$NDTMP"; echo mv "$FN" "$NDTMP"; done

remove the echoes when happy with the result.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-24-2017
Unfortunately, its not working
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What's the best way to check file permissions before moving files if needed?

Hello, I would like to know if it's a good practice to check the file permissions of the contents of a directory before moving them. For example: mv -- "$directory"/* "$directory"/.* "$directory"/..?* "$destination"The variables $directory and $destination contain the path to an existing... (6 Replies)
Discussion started by: Cacializ
6 Replies

2. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

3. Shell Programming and Scripting

Moving files based on file creation

Hi, I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir). Simply, if file is olderthan one month move it from source1 dir to BKP1 dir. My file names doesn't have... (7 Replies)
Discussion started by: karumudi7
7 Replies

4. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

5. Shell Programming and Scripting

Searching Alphanumeric Character From a File

Hi, In a error log file, the error code of a particular error contains both Alphabet and Numbers. My problem statement is to find the error codes from a particular log. That means, I need to search a word, which contains both alphabet and number. Please help me. Below is two examples of error... (1 Reply)
Discussion started by: snehasish_jana
1 Replies

6. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

Hi All, I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another. ie, this is how it will be in the file, SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Discussion started by: dsfreddie
5 Replies

7. Shell Programming and Scripting

Moving files only by oldest file one at a time

Hi I am want to create a script where the file gets moved from the current folder to a folder transfer based on the oldest first. This script should run one file at a time using a loop. I want it as a loop because I want to do some processing while I have one file. Can anyone guide me on this? (2 Replies)
Discussion started by: chamajid
2 Replies

8. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

9. Shell Programming and Scripting

ksh to compare alphanumeric values from 2 files

Hi there, I want to compare 2nd column which are alphanumeric values from each of the 2 files i.e.,lspv_pre.out and lspv_post.out , if found echo some message. lspv_pre.out hdisk0 00c39eaa451144dd rootvg active hdisk1 00c39eaa45223322 ... (3 Replies)
Discussion started by: mbak
3 Replies

10. Shell Programming and Scripting

Moving files listed in a data file to a new directory using Perl

Hi, I have a data file that lists a number of files. I want to move the files named in that one to another directory. Here's what I have: #!/usr/bin/perl -w open(FILE, "<collision.txt"); my @lines=<FILE>; foreach my $lines (@lines) { system("mv $lines collisions/."); } close(FILE); ... (2 Replies)
Discussion started by: renthead720
2 Replies
Login or Register to Ask a Question