Tag Info

New answers tagged

1

There are only two characters that are really not allowed in a filename in unix, and you've got one of them. http://stackoverflow.com/questions/457994/what-characters-should-be-restricted-from-a-unix-file-name Are the files on a usb stick formatted as some exotic file system? You really have to try hard to get a "/" into a filename in unix. ...


2

String interpolation causes this. There are a number of ways to selectively prevent this from happening. The bash hackers wiki has some good examples, though the specifics may vary if you're not actually using bash. In short, you can prevent interpolation with single quotes, or you can escape the characters. [me:~/work]$ export foo=bar [me:~/work]$ echo ...


1

Another solution: find records -type f -iname '*.mpg' | xargs rename 5.mpg 0.mpg Since all the files that you want to rename ends with *5.mpg, this find+rename combo will work just fine for you.


0

Actually I gave a try to write my own script. It seems to work: #!/bin/bash find . -type f -iname "*5.mpg" -print0 | while IFS= read -r -d $'\0' line; do new=${line//5.mpg} zero="0.mpg" buffer=$new$zero new=$buffer mv $line $new echo "Moving $line" done


1

Just loop through all *.5mpg files and use help of parameter expansion to change filenames: for file in *5.mpg; do mv -- "$file" "${file%5.mpg}"0.mpg; done To do it for different directories set globstar option (shopt -s globstar in bash) and additionally take path component with dirname command or again using parameter expansion.


2

You can use the touch command to create empty files. With crazy names like that, it's essential to quote them properly. touch ";rm *;.jpg" ";rm -rf *;.jpg" If you create files named like that on my machine your life expectancy will be very short. :)


3

There's no need to use sed, this can be handled by parameter expansion mv -- "$x" "${x//%20/ }" FWIW, I'd be replacing those %20s with an underscore (or something); I hate file names that contain spaces. But I guess learning how to write bash scripts that can handle spaces and other special characters in file names is a Good Thing. :) As Izkata mentions ...


5

Use rename and replace the %20 with a space in all type of files: $ rename -n 's/%20/ /g' * File%20with%20in00.yA2 renamed as File with in00.yA2 File%20with%20in01.h9H renamed as File with in01.h9H File%20with%20in02.CNR renamed as File with in02.CNR File%20with%20in03.PuP renamed as File with in03.PuP File%20with%20in04.js8 renamed as File with in04.js8 ...


6

Since the output of the mv probably will have spaces you need to put double quotes around the result in order not to try and execute commands in the for loop like: mv abc%20def abc def where mv has too many arguments. These are the ones giving you the usage: message. What you should do is: for x in *_MG*.CR2 do mv -- "$x" "$(printf '%s\n' "$x" | sed ...


2

GNU sort and xargs might do the trick printf '%s\0' ullman*.pbm | sort -z -k2,2n -t'-' | xargs -0 convert First check this works by listing files without calling convert printf '%s\0' ullman*.pbm | sort -z -k2,2n -t'-' | xargs -0 printf '%s\n' ullman-000.pbm ullman-001.pbm ullman-098.pbm ullman-099.pbm ullman-100.pbm ullman-1000.pbm ... Whereas ...


1

One way would be to rename the files that have three digit numbers to four digit ones, padded with a zero. If you have perl-rename (installed by default on Ubuntu) you can try: rename -n 's/-(\d{3}\.)/-0$1/' *.pbm Once you're satisfied with the result, run again without the -n. Or see other options in Padding a number in a filename to a fixed length.


1

Why not just... for log in /var/log/*.[1-5] do whatever to "$log" done You don't need find as far as I can tell - the shell uses the same globs it does in -name. And if all of the files are in a single directory... Of course, if there are subdirectories you're also interested in then find could be beneficial - walking trees in the shell can be a headache. ...


1

Use the shell's suffix removal feature str=/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr path=${str%/*} echo "$path" In general, ${parameter%word} removes word from the end of parameter. In our case, we want to remove the final slash and all characters which follow: /*. The above produces: /opt/oracle/app/oracle/product/12.1.0/bin Use dirname ...


0

start cmd:> dirname "/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr" /opt/oracle/app/oracle/product/12.1.0/bin file_path="/opt/oracle/app/oracle/product/12.1.0/bin/tnslsnr" dir_path_woslash="${file_path%/*}" echo "$dir_path_woslash" /opt/oracle/app/oracle/product/12.1.0/bin shopt -s extglob dir_path_wslash="${file_path%%+([^/])}" echo ...


3

In addition to @Stéphane Chazelas's answer, we don't have to worry about this issue if we use -i command line option: $ perl -pe '' 'uname|' Linux $ perl -i -pe '' 'uname|' Can't open uname|: No such file or directory. Because when using -i option, perl used stat to check the file status before process it: $ strace -fe trace=stat perl -pe '' 'uname|' ...


14

What's the problem First, like for many utilities, you'll have an issue with file names starting with -. While in: sh -c 'inline sh script here' other args The other args are passed to the inline sh script; with the perl equivalent, perl -e 'inline perl script here' other args The other args are scanned for more options to perl first, not to the ...


0

You are not using regular expressions correctly. tes* means te with any number of ss following it, so test-file-1 will be renamed to file 1t-file-1: $ rename -n 's/tes*/file 1/' * test-file-1 renamed as file 1t-file-1 Similarly, ^* will match the empty string appearing at the start, so in effect it's like ^, but with a an inifinite loop: $ rename -n ...


2

Recursing into subdirectories Parsing the output of find is unreliable. What if there was a file name with a newline in it? Use find … -exec …, which guarantees reliable processing. find . -type f -exec sh -c '…' {} \; The shell snippet … receives the file name in $0. Note that this is a separate shell process, it doesn't inherit variables or functions ...


1

While the general problem of identifying extensions is hard, you can clean up the script a bit: Tell find to only consider files with an extension: -iname '*.*' Use awk instead of cutting yourself: Use a script, and then tell find to exec that script. Thus: a script called, say, move.sh: #! /bin/bash for i do ext=/some/where/else/$(awk -F. '{print ...



Top 50 recent answers are included