Using 'find' to return filenames without extension
To return only filenames without the extension, try:
find . -type f -iname "*.ipynb" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';'
or (omitting -type f
from now on):
find "$PWD" -iname "*.ipynb" -execdir basename {} .ipynb ';'
or:
find . -iname "*.ipynb" -exec basename {} .ipynb ';'
or:
find . -iname "*.ipynb" | sed "s/.*\///; s/\.ipynb//"
however invoking basename
on each file can be inefficient, so @CharlesDuffy suggestion is:
find . -iname '*.ipynb' -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} +
or:
find . -iname '*.ipynb' -execdir basename -s '.sh' {} +
Using +
means that we're passing multiple files to each bash instance, so if the whole list fits into a single command line, we call bash only once.
To print full path and filename (without extension) in the same line, try:
find . -iname "*.ipynb" -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';'
or:
find "$PWD" -iname "*.ipynb" -print | grep -o "[^\.]\+"
To print full path and filename on separate lines:
find "$PWD" -iname "*.ipynb" -exec dirname "{}" ';' -exec basename "{}" .ipynb ';'
find filenames NOT ending in specific extensions on Unix?
Or without (
and the need to escape it:
find . -not -name "*.exe" -not -name "*.dll"
and to also exclude the listing of directories
find . -not -name "*.exe" -not -name "*.dll" -not -type d
or in positive logic ;-)
find . -not -name "*.exe" -not -name "*.dll" -type f
Extract filename and extension in Bash
First, get file name without the path:
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:
filename="${fullfile##*/}"
You may want to check the documentation :
- On the web at section "3.5.3 Shell Parameter Expansion"
- In the bash manpage at section called "Parameter Expansion"
how to retreive a list of filenames from a directory without the file extension
Just use:
(Get-ChildItem | Select -Expand BaseName) >filenames.txt
Basename
property gets the file name without extension.
Getting file names without extensions
You can use Path.GetFileNameWithoutExtension
:
foreach (FileInfo fi in smFiles)
{
builder.Append(Path.GetFileNameWithoutExtension(fi.Name));
builder.Append(", ");
}
Although I am surprised there isn't a way to get this directly from the FileInfo
(or at least I can't see it).
How to get a list of filenames(without extension) in directory in python?
import os
files_no_ext = [".".join(f.split(".")[:-1]) for f in os.listdir() if os.path.isfile(f)]
print(files_no_ext)
How can I remove the extension of a filename in a shell script?
You should be using the command substitution syntax $(command)
when you want to execute a command in script/command.
So your line would be
name=$(echo "$filename" | cut -f 1 -d '.')
Code explanation:
echo
get the value of the variable$filename
and send it to standard output- We then grab the output and pipe it to the
cut
command - The
cut
will use the . as delimiter (also known as separator) for cutting the string into segments and by-f
we select which segment we want to have in output - Then the
$()
command substitution will get the output and return its value - The returned value will be assigned to the variable named
name
Note that this gives the portion of the variable up to the first period .
:
$ filename=hello.world
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello.hello.hello
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello
$ echo "$filename" | cut -f 1 -d '.'
hello
How do I get the filename without the extension from a path in Python?
Getting the name of the file without the extension:
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
Prints:
/path/to/some/file
Documentation for os.path.splitext
.
Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example:
import os
print(os.path.splitext("/path/to/some/file.txt.zip.asc")[0])
Prints:
/path/to/some/file.txt.zip
See other answers below if you need to handle that case.
Return only certain filename without extension
You can use str.endswith passing a tuple of extensions:
f.extend((os.path.splitext(name)[0] for name in filenames if name.lower().endswith((".jpeg", ".gif")))
I am not sure why you have a break in your for loop as that will mean you loop over one directory which you could be doing with os.listdir
Related Topics
What Does "Bash:No Job Control in This Shell" Mean
Shellscript to Monitor a Log File If Keyword Triggers Then Execute a Command
Ld: Using -Rpath,$Origin Inside a Shared Library (Recursive)
Git Enter Long Passphrase for Every Push
What Occurs When a File Is 'Source'-D in Unix/Linux Context
.Htaccess Not Working (Mod_Rewrite)
Two File Descriptors to Same File
Bash: How to Tokenize a String Variable
How to Get the Last Word in Each Line with Bash
Git: Can't Push (Unpacker Error) Related to Permission Issues
Encrypt a String Using Openssl Command Line
Why Do We Need a Swapper Task in Linux
Call a Kernel Module Function from Program at User Space
Pipe Output to Use as the Search Specification for Grep on Linux
How to Convert Spaces to Tabs in Vim or Linux