linux:start

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:start [2026/04/26 22:17] mhlinux:start [2026/07/02 21:26] (current) – [File operations] mh
Line 220: Line 220:
 === Mass renaming === === Mass renaming ===
  
-Uppercase .JPG to lowercase .jpg+__**Changing an extension:**__
  
 <code bash> <code bash>
Line 226: Line 226:
 </code> </code>
  
-Alternative (rename tool):+__**Changing a prefix:**__ 
 + 
 +<code bash> 
 +for i in "prefix"*.png; do 
 +  mv -n -- "$i" "${i/#prefix/another prefix}" 
 +done 
 +</code> 
 + 
 +  * ''"prefix"*.png'' targets only files beginning with the selected prefix, so unrelated files in the folder are ignored. 
 +  * ''${i/#prefix/another prefix}'' replaces only the prefix at the beginning of the filename. If the same text appears later in the filename, it is left untouched. 
 +  * In Bash parameter expansion, the ''#'' before ''prefix'' means “match only at the start of the variable value”. Without ''#'', Bash would replace the first matching occurrence anywhere in the filename. 
 +  * ''mv -n'' prevents overwriting an existing file if the renamed target already exists. 
 +  * ''--'' tells ''mv'' that everything after it is a filename, not an option. This protects against unusual filenames starting with ''-''
 +  * ''"$i"'' keeps the original filename safely quoted, so spaces and special characters in filenames are preserved. 
 + 
 +__**Alternative (rename tool):**__
  
 <code bash> <code bash>
 rename 's/\.JPG$/.jpg/' *.JPG rename 's/\.JPG$/.jpg/' *.JPG
 </code> </code>
 +
 +  * Use the ''for ... done'' + ''mv -n --'' loop for safe, explicit prefix renaming.
 +  * Use ''rename'' for concise regex-based changes, especially extension changes such as ''.JPG'' to ''.jpg''.
 +  * On Linux Mint / Ubuntu, ''rename 's/\.JPG$/.jpg/' *.JPG'' usually works because the available ''rename'' is the Perl-style tool.
 +  * The ''rename'' command is not fully portable across all Linux distributions, because different systems may ship different versions with different syntax.
 +  * For safety, preview first with ''rename -n 's/\.JPG$/.jpg/' *.JPG''.
  
 ---- ----
  • linux/start.1777234644.txt.gz
  • Last modified: 2026/04/26 22:17
  • by mh