Swapping file and directory names

Charles

I have a collection of files at ./date-and-time/fixed/path/filename where date-and-time and filename are variable. I would like to move all these files to ./filename/date-and-time. The former path is the filename and the former filename is the path. Is there a good way to do this? There are ~1000 files in total with ~100 distinct filenames.

steeldriver

Something like this should work (note I have echoed the actual commands; please check carefully that it is doing the right thing before removing them)

#!/bin/bash

shopt -s nullglob

for file in */fixed/path/*; do 
  [[ -f "$file" ]] || continue
  f="${file##*/}"; d="${file%%/*}"
  echo mkdir -p "$f" && echo mv --no-clobber -- "$file" "$f/$d"
done

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

loop through directory names using a batch file?

From Dev

Inno Setup: List all file names in an directory

From Dev

Finding repeated file names in directory (without specifying the exact file name)

From Dev

linux cpio to copy directory structure and file names?

From Dev

Powershell file and directory names as strings, sans decorations

From Dev

Unable to output file names from directory in PHP

From Dev

How to build a list of file names in a local directory?

From Dev

Python: Print file names and their directory based on their file size

From Dev

How to save to txt file current directory + file names

From Dev

Swapping dictionary keys and column names

From Dev

Use of find in unix on strange file/directory names

From Dev

Delete files and directories by their names. No such file or directory

From Dev

Stripping directory paths to get file names

From Dev

Unique file names in a directory in unix

From Dev

Finding repeated file names in directory (without specifying the exact file name)

From Dev

List file names from a directory and place output in a text file

From Dev

Getting directory names and writing them to a text file

From Dev

How to zip directory with encryption for file names?

From Dev

File System that supports duplicate directory names

From Dev

Checking file names inside a directory with Regular Expressions

From Dev

Creating .finished file with the folder names under a directory

From Dev

change file names in a directory with a certain pattern at the beginning

From Dev

Obtaining file names from directory in Bash

From Dev

echo names and number of lines for each file in a directory

From Dev

Renaming 100 files in a directory to new file names stored in a text file

From Dev

Directory/file names confusion

From Dev

Read unknown file names from directory

From Dev

Extracting directory / file names in a Makefile

From Dev

Importing file names from directory to listbox

Related Related

  1. 1

    loop through directory names using a batch file?

  2. 2

    Inno Setup: List all file names in an directory

  3. 3

    Finding repeated file names in directory (without specifying the exact file name)

  4. 4

    linux cpio to copy directory structure and file names?

  5. 5

    Powershell file and directory names as strings, sans decorations

  6. 6

    Unable to output file names from directory in PHP

  7. 7

    How to build a list of file names in a local directory?

  8. 8

    Python: Print file names and their directory based on their file size

  9. 9

    How to save to txt file current directory + file names

  10. 10

    Swapping dictionary keys and column names

  11. 11

    Use of find in unix on strange file/directory names

  12. 12

    Delete files and directories by their names. No such file or directory

  13. 13

    Stripping directory paths to get file names

  14. 14

    Unique file names in a directory in unix

  15. 15

    Finding repeated file names in directory (without specifying the exact file name)

  16. 16

    List file names from a directory and place output in a text file

  17. 17

    Getting directory names and writing them to a text file

  18. 18

    How to zip directory with encryption for file names?

  19. 19

    File System that supports duplicate directory names

  20. 20

    Checking file names inside a directory with Regular Expressions

  21. 21

    Creating .finished file with the folder names under a directory

  22. 22

    change file names in a directory with a certain pattern at the beginning

  23. 23

    Obtaining file names from directory in Bash

  24. 24

    echo names and number of lines for each file in a directory

  25. 25

    Renaming 100 files in a directory to new file names stored in a text file

  26. 26

    Directory/file names confusion

  27. 27

    Read unknown file names from directory

  28. 28

    Extracting directory / file names in a Makefile

  29. 29

    Importing file names from directory to listbox

HotTag

Archive