Extract file basename without path and extension in bash [duplicate]

You don’t have to call the external basename command. Instead, you could use the following commands: $ s=/the/path/foo.txt $ echo “${s##*/}” foo.txt $ s=${s##*/} $ echo “${s%.txt}” foo $ echo “${s%.*}” foo Note that this solution should work in all recent (post 2004) POSIX compliant shells, (e.g. bash, dash, ksh, etc.). Source: Shell Command Language … Read more

Getting the filenames of all files in a folder [duplicate]

You could do it like that: File folder = new File(“your/path”); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { System.out.println(“File ” + listOfFiles[i].getName()); } else if (listOfFiles[i].isDirectory()) { System.out.println(“Directory ” + listOfFiles[i].getName()); } } Do you want to only get JPEG files or all files?

How do I remove the file suffix and path portion from a path string in Bash?

Here’s how to do it with the # and % operators in Bash. $ x=”/foo/fizzbuzz.bar” $ y=${x%.bar} $ echo ${y##*/} fizzbuzz ${x%.bar} could also be ${x%.*} to remove everything after a dot or ${x%%.*} to remove everything after the first dot. Example: $ x=”/foo/fizzbuzz.bar.quux” $ y=${x%.*} $ echo $y /foo/fizzbuzz.bar $ y=${x%%.*} $ echo $y … Read more

What characters are forbidden in Windows and Linux directory names?

The forbidden printable ASCII characters are: Linux/Unix: / (forward slash) Windows: < (less than) > (greater than) : (colon – sometimes works, but is actually NTFS Alternate Data Streams) ” (double quote) / (forward slash) \ (backslash) | (vertical bar or pipe) ? (question mark) * (asterisk) Non-printable characters If your data comes from a … Read more

How to loop over files in directory and change path and add suffix to filename

A couple of notes first: when you use Data/data1.txt as an argument, should it really be /Data/data1.txt (with a leading slash)? Also, should the outer loop scan only for .txt files, or all files in /Data? Here’s an answer, assuming /Data/data1.txt and .txt files only: #!/bin/bash for filename in /Data/*.txt; do for ((i=0; i<=3; i++)); … Read more

Extracting extension from filename in Python

Use os.path.splitext: >>> import os >>> filename, file_extension = os.path.splitext(‘/path/to/somefile.ext’) >>> filename ‘/path/to/somefile’ >>> file_extension ‘.ext’ Unlike most manual string-splitting attempts, os.path.splitext will correctly treat /a/b.c/d as having no extension instead of having extension .c/d, and it will treat .bashrc as having no extension instead of having extension .bashrc: >>> os.path.splitext(‘/a/b.c/d’) (‘/a/b.c/d’, ”) >>> os.path.splitext(‘.bashrc’) … Read more

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 “https://stackoverflow.com/” 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” … Read more

tech