2

Excuse the newby question but I have a number of unique directories which each contain a single file of a generic name.

enter image description here

What I would like to do is to move nd rename the files into a single directory

Preferably using wildcards but I guess you could also generate a list and cycle through it.

Help would be greatly appreciated.

2 Answers 2

0

try to do something like this,

for d in ${PATH_OF_YOUR_DIRECTORY}/*/ ; do
    mv ${d}index.html ${PATH_WHERE_YOU_HAVE_TO_MOVE}/$(basename ${d}).html
done
5
  • Is this a shell script or can it be entered directly in the terminal?
    – AusS2000
    Mar 28, 2017 at 3:23
  • you can use it in both the ways dear ! try it @AusS2000
    – Prem Joshi
    Mar 28, 2017 at 3:42
  • 1
    Cheers, that hit the spot! (I was having a problem with the $ at the start of the path as my paths were absolute. I removed them and it worked)
    – AusS2000
    Mar 28, 2017 at 4:05
  • I'm glad !! keep scripting !!
    – Prem Joshi
    Mar 28, 2017 at 4:18
  • 1
    Hi @AusS2000 if the answer solved your problem, as you indicated, please consider to accept the answer (tick the big "V" below the up/down arrows on the left). It is the appropriate way to indicate the answer worked for you. Mar 28, 2017 at 5:39
0

It's not clear from the question how the files should be renamed, so I'm going to assume that they should be renamed by appending the name of their original directory to their original name.

The code below assumes that your working directory holds the subdirectories and each subdirectory has the index.html file, as shown in the question. The index.html files will be moved to a new directory called allfiles.

mkdir allfiles || exit

for pathname in */index.html
do
    mv -- "$pathname" "allfiles/${pathname%/index.html}-index.html"
done

The pathname variable will hold pathnames like A-Dwelling-Place/index.html, and the parameter expansion ${pathname%/index.html} would remove the /index.html bit from the end of that pathname.

You could instead iterate over the directories rather than the files,

mkdir allfiles || exit

for dirname in */
do
    mv -- "$dirname"/index.html "${dirname%/}-index.html"
done

In this case, the dirname variable would hold pathnames like A-Dwelling-Place/, and ${dirname%/} would delete that trailing slash character.

You must log in to answer this question.

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