You are not logged in.

#1 2023-04-19 22:45:14

JasonMehmel
Member
Registered: 2015-10-06
Posts: 186

renaming files that don't currently have a file extension?

I hope this is sufficently sys-admin-y to belong here.

I wanted to rename all the files within a set of directories, recursively, from .txt to .md

This was so I could try to open the whole directory under obsidian for some knowledge management workflow stuff.

I used this:

find . -iname "*.txt" -exec sh -c 'mv "$0" "${0%\.txt}.md"' {} \;

And that worked great for all the .txt files. But there are a few plain text files that don't have an extension. Either I named them without thinking about the extension or they were holdovers from other systems that I held these files on. They open fine in an editor.

What I'm wondering: could I use the same command above for non-extension plain text files?

Something like this?

find . -iname "*." -exec sh -c 'mv "$0" "${0%\.}.md"' {} \;

I'd be using *. to search all the available files that don't have an extension, and the hope is that the "$0" "${0%\.}.md"' section works just by replacing the .[nothing] with a .md.

But I'm worried that if I try this, I'll just change the extension of every file into an .md file, which I don't want! Just the non-file-extension files, which are all plain text files.


Fortune favours the bold.
ThinkPad T15 Gen 2i

Offline

#2 2023-04-20 02:01:20

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,550
Website

Re: renaming files that don't currently have a file extension?

To filter only the files with no dot anywhere in their names this seems to work:

find . ! -iname '*.*'

As long as you're sure the files you want have no dot anywhere, that ought to be OK.

Then, the command to run. This should be something like:

-exec sh -c ' do stuff to "$1" ' _ {} \;

See Greg's wiki on using find: https://mywiki.wooledge.org/UsingFind

The mini-script is followed by _ and {}. When bash -c executes a command, the next argument after the command is used as $0 (the script's "name" in the process listing), and subsequent arguments become the positional parameters ($1, $2, etc.). This means that the filename passed by find (in place of the {}) becomes the first parameter of the script -- and is referenced by $1 inside the mini-script. (Don't omit the _ and try to use $0 inside the mini-script -- not only would that be more confusing, but it is also prone to failure if the filename provided by find has special meaning as an argument to the shell.)

So this might work (try it on a test directory first):

find . ! -iname '*.*' -exec sh -c 'mv "$1" "${1}.md"' _ {} \;

---
Greg's wiki on renaming files:
https://mywiki.wooledge.org/BashFAQ/030


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#3 2023-04-26 19:05:57

JasonMehmel
Member
Registered: 2015-10-06
Posts: 186

Re: renaming files that don't currently have a file extension?

I ended up finding these through some other research, but it lines up mostly with what you provided, thank you!

rename all .txt file extensions in directory and recursively

find . -iname "*.txt" -exec sh -c 'mv "$0" "${0%\.txt}.md"' {} \;

rename all files with no file extensions in directory and recursively

find . -type f ! -name "*.*" -exec sh -c 'mv "$0" "$0.md"' {} \;

Last edited by JasonMehmel (2023-06-30 19:41:13)


Fortune favours the bold.
ThinkPad T15 Gen 2i

Offline

#4 2023-04-27 03:02:52

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,550
Website

Re: renaming files that don't currently have a file extension?

Except the bit about using $1 not $0.

Don't omit the _ and try to use $0 inside the mini-script -- not only would that be more confusing, but it is also prone to failure if the filename provided by find has special meaning as an argument to the shell.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#5 2023-04-27 22:31:40

JasonMehmel
Member
Registered: 2015-10-06
Posts: 186

Re: renaming files that don't currently have a file extension?

Well, I did say mostly... wink

If I read that right, the same command-script would still work with $1 replacing all of the $0's?


Fortune favours the bold.
ThinkPad T15 Gen 2i

Offline

#6 2023-04-28 02:30:06

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,550
Website

Re: renaming files that don't currently have a file extension?

Almost - if you look carefully, there's an extra _  argument to the "mini-script". That occupies $0 so {} becomes $1.
https://mywiki.wooledge.org/UsingFind


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

Board footer

Powered by FluxBB