0

I have a directory AllData with some files like below:

AllData
 |____ file_1to1000.track
 |____ file_1001to2000.track
 |____ file_2001to3000.track

Based on file names I created directories with file names.

for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done

And I wanted to create another directory inside that and keep the file. It should like below:

AllData
 |__ file_1to1000
          |___ cuffcompare
                   |____ file_1to1000.track

 |__ file_1001to2000
          |___ cuffcompare
                   |____ file_1001to2000.track

 |__ file_2001to3000
          |___ cuffcompare
                   |____ file_2001to3000.track

After moving the files into directories like above, the file name should be changed from file_1to1000.track to soft.track. All the files inside directories needs to be changed from their original filename to soft.track It should basically look like below:

AllData
 |__ file_1to1000
          |___ cuffcompare
                   |____ soft.track

 |__ file_1001to2000
          |___ cuffcompare
                   |____ soft.track

 |__ file_2001to3000
          |___ cuffcompare
                   |____ soft.track

1 Answer 1

2

Check:

for file in ./*; do
    echo mkdir -p "${file%.*}"/cuffcompare/ && \
    echo mv "$file" "$_"soft.track
done

to liner:

for file in ./*; do echo mkdir -p "${file%.*}"/cuffcompare/ && echo mv "$file" "$_"soft.track; done

Note: Remove echo at above when you were happy with dry-run result.


${file%.*} strips the shortest matched suffix from the filename. so it cuts .track from the end of filename here. known Parameter-expansion

$_ is the substitute of the last argument of the previous command (see shell Special Parameters); which expands to "${file%.*}"/cuffcompare/;

so mkdir creates the directories (-p is used to create parent directories if those were not exist) structure below for every file found;

└── file_2001to3000
    └── cuffcompare

then mv moves and rename the file to its related directory with soft.tarck name:

└── file_2001to3000
    └── cuffcompare
        └── soft.track
0

You must log in to answer this question.

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