this post was submitted on 10 Nov 2023
2 points (100.0% liked)

Emacs

313 readers
2 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

Here's something I can't figure out: When using dired to manage files, let's say I have a top level directory with a ton of subdirectories, each with a GoPro video inside (unique name/time/date for each file name). How do I move them all at once to that top level directory for easier management/renaming? I don't want to have to go into each directory and move them one at a time with R. Let's say all of the files are MP4 or HEVC.

top 15 comments
sorted by: hot top controversial new old
[–] Bldck@beehaw.org 1 points 1 year ago
find /path/to/parent/directory -type f \( -name "*.mp4" -o -name "*.hvec" \) -exec mv -t /path/to/parent/directory {} +

Explanation:

•	find /path/to/parent/directory: Start the search from the specified parent directory.
•	-type f: Restrict the search to files (not directories).
•	\( -name "*.mp4" -o -name "*.hvec" \): Look for files with either a .mp4 or .hvec extension.
•	-exec mv -t /path/to/parent/directory {} +: Execute the mv command to move the found files to the specified parent directory.
[–] Illiamen@alien.top 1 points 1 year ago (1 children)

You could use M-x find-name-dired to list the files matching a pattern in the directory and subdirectories in a Dired buffer. See: https://www.gnu.org/software/emacs/manual/html_node/emacs/Dired-and-Find.html

The manual also says that you could use the recursive flag for ls, but I've never tried it. See: https://www.gnu.org/software/emacs/manual/html_node/emacs/Subdirectories-in-Dired.html

[–] TyrionBean@alien.top 1 points 1 year ago

I'll give it a shot. Thank you. Sounds like what I need (and what others have suggested as well).

[–] xenodium@alien.top 1 points 1 year ago (1 children)

I can think of two options:

  1. M-x find-dired RET RET RET (or adjust query): which will list all files recursively in dired. You probably don't need to copy them elsewhere for renaming. Use M-x dired-toggle-read-only (or C-x C-q), rename in-place, and commit (toggle again C-x C-q). https://xenodium.com/batch-renaming-with-counsel-find-dired-and-wdired
  2. dired-subtree: enables drilling down to multipe subdirectories from the same dired buffer. Expand the subdirectories needing management, and edit like 1. (via C-x C-q) https://xenodium.com/drill-down-emacs-dired-with-dired-subtree

Edit: markup

[–] TyrionBean@alien.top 1 points 1 year ago

I'll give it a try. Thank you Xenodium. Everyone seems to have pointed me in this direction. I knew Dired would have something like this.

[–] sceadu@alien.top 1 points 1 year ago (3 children)

I know this is the emacs subreddit but I think it would be a better task for just the CLI 'find' command with mindepth and exec

[–] centzon400@alien.top 1 points 1 year ago (1 children)
[–] TyrionBean@alien.top 1 points 1 year ago (1 children)

I'm saving that snippet for later use. Thank you.

[–] sceadu@alien.top 1 points 1 year ago

Yeah I was thinking just do the move in the CLI as a trivial step and use dired for bulk remaining (wdired mode)

[–] TyrionBean@alien.top 1 points 1 year ago

Yeah unfortunately, CLI scripting can get a little complex for me at times, and I knew that dired would have this built in somehow.

[–] arthurno1@alien.top 1 points 1 year ago

I think it would be a better task for just the CLI

No, it wouldn't :). You can certainly do it, but it is frankly, easier with Emacs only.

[–] FrozenOnPluto@alien.top 1 points 1 year ago (1 children)

I dunno, sounds like a one liner in cli .. unless you need to do this many times in the future or just want to force it into Emacs for fun (which is okay :), strikes me as a 10s thing to do on cli if you’rr a Mac or Unix-like user

cd /to/top mv */*mov .

Repeat for other file type.

That assumes they’re only one level deep

Another one liner ..

find /top -name “*mov” -exec “mv {} /top” ;

Be careful as just tapping out on phone but you get the idea

[–] TyrionBean@alien.top 1 points 1 year ago

Ah, this sounds even simpler. I. hadn't thought of this, but I was doing everything in dired for management/renaming, so I hadn't thought of doing something like this in the CLI. I'll make a note of it. Much appreciated.

[–] Aminumbra@alien.top 1 points 1 year ago

For an elisp solution (not dired, still in Emacs):

             (rename-file filename ))
           (directory-files-recursively  "."))

You can filter via the second argument of directory-files-recursively which files you want to list (using a regexp), or use any test you want in the lambda.

Not the fastest if you have a large amount of files, as it is pure elisp.

[–] arthurno1@alien.top 1 points 1 year ago

You don't need to move files around just to bulk rename them. If you want to do it, sure, you can, but you don't have to.

In your top level directory:

  1. C-u C-x d (alternatively C-u M-x dired)

You will be in minibuffer now. Be sure dired listing switches have -l and -R option; -l is probably already there among anything else you might use; just type space and add -R at the end and type Return (Enter)

Now you will have a Dired buffer with al the subdirectories and files in them listed in the same buffer.

  1. C-x C-q in Dired to switch to WDired mode (I have bound it in my Emacs to C-S-r for easier typing)

You are now in "writeable dired" mode where you can edit all file names as if it were an ordinary text buffer. You can also use replace-string for example to replace a part of the name in all filenames at once, regex-replace, etc. You can do it manually, or whatever else you would do in a text buffer.

  1. C-c C-c to save your changes when you are done; all files will be updated.