5

I have multiple files in one folder:

ABA.xy
BEB.xy
ACA.xy

The same files with different extension in another folder

ABA.rr
BEB.rr
ACA.rr

Then I have single folder for each file name

ABA
BEB
ACA

I would like to do the following. if the file with extension .xy and .rr have the same name as the folder, move to that folder. For example

ABA.xy and ABA.rr in ABA

I am familiar with mv but I think that here I have to write a small script to do that.

8
  • What to do with filenames without extension? Jan 30, 2015 at 12:24
  • this are folder not files
    – efrem
    Jan 30, 2015 at 12:30
  • You are referring to files in the first two sections of your question (with different extension). Jan 30, 2015 at 12:31
  • Yes I have files with the same name but different extensions. Then I have folders with the same name of the files. I want to move the files whatever extension they have in the folder that has a similar name.
    – efrem
    Jan 30, 2015 at 12:33
  • 1
    I think I quite do not get what you are asking. All my files are either .xy or .rr
    – efrem
    Jan 30, 2015 at 12:37

2 Answers 2

9

Assuming your files stored in source dir and your target folders are in dest like following tree:

$ tree source
source
├── dir1
│   ├── ABA.xy
│   ├── ACA.xy
│   └── BEB.xy
└── dir2
    ├── ABA.rr
    ├── ACA.rr
    └── BEB.rr
2 directories, 6 files

$ tree dest
dest
├── ABA
├── ACA
└── BEB
3 directories, 0 files

The command would be:

find source -type f -exec sh -c 'noext="${0%.*}"; echo mv '{}' "dest/${noext##*/}/"' {} \;
  • the source is the source directory or specify the parent directory.
  • noext="${0%.*}" strips the files extension and saves the result into noext variable.
  • "${noext##*/}" removes the file path portion from noext variable and pickup the only file name.

  • dest directory is destination parent directory.

  • So mv '{}' "dest/${noext##*/}/"' moves the current file to dest/$noext directory.

    The content of noext in above command is only filename without extension and path in it that specify the destination directory which is the same name as the current file name.

Here is a sample test that show the result of command

Note that this commands used echo for testing and dry run. If you ensure about what you are do, remove the echo in front of mv command to perform actual moving.

0
2

A slightly different approach using rename:

rename 's#.*/(.+)(\.\w+)#mkdir "dest/$1"; "dest/$1/$1$2"#e' source/{a,b}/*

This is essentially just using a regular expression to translate the paths. We use a little bit of Perl (which rename supports) to create the new directories and rename moves the file at the end.

It isn't too clever. It won't search for cases where there isn't a corresponding file (we could search and remove directories with only one file) and it'll probably explode if there are files without extensions.

$ mkdir -p source/{a,b} dest
$ touch source/a/{ABA,BEB,ACA}.xy
$ touch source/b/{ABA,BEB,ACA}.rr
$ tree
.
├── dest
└── source
    ├── a
    │   ├── ABA.xy
    │   ├── ACA.xy
    │   └── BEB.xy
    └── b
        ├── ABA.rr
        ├── ACA.rr
        └── BEB.rr

$ rename 's#.*/(.+)(\.\w+)#mkdir "dest/$1"; "dest/$1/$1$2"#e' source/{a,b}/*
$ tree
.
├── dest
│   ├── ABA
│   │   ├── ABA.rr
│   │   └── ABA.xy
│   ├── ACA
│   │   ├── ACA.rr
│   │   └── ACA.xy
│   └── BEB
│       ├── BEB.rr
│       └── BEB.xy
└── source
    ├── a
    └── b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .