New answers tagged

0

I wrote two functions you can use together that do just that, you can limit the directory level by adding a -maxdepth $VAL parameter. # This scripts flattens the file directory # Run this script with a folder as parameter: # $ path/to/script path/to/folder #!/bin/bash rmEmptyDirs(){ local DIR="$1" for dir in "$DIR"/*/ do [ -d "${dir}" ]...


3

Solution using Parameter Expansion for i in *.mp3 ; do mv "$i" "${i#${i%%[!0-9 ]*}}" ; done ${i%%[!0-9 ]*} deletes from first non-digit/non-space character till end. Remaining characters are passed to ${i# which then deletes them from beginning of i variable resulting in desired file name without starting digits and space


6

I don't see why that error would occur. In fact, I am reasonably certain there were more lines to the error than you show; for one thing, there's no actual error message. However, that regular expression won't actually match either of your example files. You are using [a-z]+\.mp3 which will only match lower case letters and, since you're matching all the ...


0

The following sh shell loop will remove all spaces, underscores and dashes from the names of files in the current directory, taking care to not overwrite any existing files: for f in *; do test -f "$f" || continue nf=$( echo "$f" | tr -d ' _-' ) ! test -e "$nf" && echo mv "$f" "$nf" done For bash and ksh, and being slightly more ...


3

bash solution (provided extglob shell option is enabled - see pattern matching manual) for i in 0.* ; do mv "$i" "${i##0.*(0)}" ; done ${i## delete longest match from beginning of i variable 0. matches the character sequence 0. *(0) means zero or more occurrences of 0 or this solution suggested by @Costas, which doesn't need the extglob option for i ...


1

With rename (prename): rename -n 's/^[^.]+\.0*([1-9]+)$/$1/' 0* -n will do the dry-run, if you are satisfied with the changes to be made, do: rename 's/^[^.]+\.0*([1-9]+)$/$1/' 0* Example: % rename -n 's/^[^.]+\.0*([1-9]+)$/$1/' 0* 0.001 renamed as 1 0.002 renamed as 2 0.003 renamed as 3 0.035 renamed as 35


3

Assuming your shell supports parameter expansion: for f in *_*_*; do mv -i "$f" "${f#*_}"; done The parameter expansion pattern, ${f#*_}, removes the substring upto first _ from left Or using rename (prename): rename 's/^[^_]*_(.*)/$1/' *_*_* ^[^_]*_ matches the substring upto first _ from start and (.*) matches the rest, it is also put into the ...


2

When you pass an argument beginning with a dash to a command, and you don't want this argument to be interpreted as an option, pass -- (double dash) as an argument first. With almost all commands, -- means “no more options after this”. rename.ul -- '-' '' -* Another possibility is to arrange for the argument not to start with a dash. This isn't always ...


4

for f in -*.mp3; do echo mv -- "$f" "${f:1}" done When you are sure that it does what you want it to do, remove the echo. The double dash (--) is necessary to stop mv from interpreting the - in the filenames as an option. Quoting the variables is necessary for the cases where the filenames contains spaces.


0

I have this issue for several unix systems with no source control with a requirement to make small changes fast. For cases like this I hit the file with a script to cut a time stamped copy of it to allow for quick recovery. ls shows the files with the versions date sorted by file name after it. if [ "$1" = "" ]; then echo "File name required. Aborting." ...


2

This should work and be shell-agnostic, run it as a script with a single parameter which is your base directory. It uses find to get the names of directories one per line, assuming they are located immediately below the supplied base directory and excluding any that have already been converted. It uses awk to strip the base directory from the list, leaving ...


3

cp should do what you want. The problem is that you are not iterating through a folder. You are only doing one iteration with the "folder" being the contents of the $file variable. Try iterating over the file globbing, like this: for file in folder/* do cp -vf 'image.png' "$file" done I added a -v so you can get more verbose output to see any error, ...


0

Using awk: awk '{split($2, p, "."); system(sprintf("wget -O %s.%s %s\n", $1, p[length(p)], $2))}' images.txt Break down First we use awk to print out roughly the command we want: awk '{printf("wget -O %s %s\n", $1, $2)}' images.txt Then we extract the extension by splitting the URL on '.' and append it to the first argument: awk '{split($2, p, "."); ...


3

This might do your job, while read a b do wget "$b" -O "$a".jpg printf "$a".jpg"\n%s" >> newfile done < images.txt


2

To test: $ awk '{ ext=gensub(/.*(\.[^.]*$)/,"\\1",1); print "wget " $2 " -O " $1 ext}' images.txt wget image.jpg -O some_id1.jpg wget image2.jpg -O some_id2.jpg wget image3.jpg -O some_id3.jpg To run, pipe into bash or sh like so: awk '{ ext=gensub(/.*(\.[^.]*$)/,"\\1",1); print "wget " $2 " -O " $1 ext}' images.txt | bash Add single-...


1

try rename -n 's/Centrum.text.(.*)/Centrum.$1.text/' Cen* for a preview s/Centrum.text.(.*)/Centrum.$1.text/ instruct to replace (moving en pattern inside) then rename 's/Centrum.text.(.*)/Centrum.$1.text/' Cen* Edit: if you don't have rename, and from directory containing files ls -1 | awk -F. '/Centrum/ {printf "mv %s.%s.%s %s.%s.%s\n",$1,$2,$...


2

Instead of messing with find + xargs + mv just switch to zsh and do autoload -U zmv zmv -n '$(MY_DIR)/source/**/(*).wav3' '$(MY_DIR)/source/${1%%.*}.wav' How it works: first we load zmv via autoload -n parameter is to prevent execution, just see what it will do, and if you are happy with the output remove this option double star ** match all nested ...


0

Using cygwin, my version of rename does not have the regex replacement, nor does its rename [options] expression replacement file... syntax seem to work. Also, the bash suggestions fail with ... -c: line 0: unexpected EOF while looking for matching `"' ... -c: line 1: syntax error: unexpected end of file Alas... there is always perl: perl -MFile::Find -...


0

This is my solution to adding a time stamp when moving files in bash #!/usr/bin/env bash cd "FILES_LOCATION" COPYDIR="NEW_FILE_LOCATION" for file in *.FILE_EXTENSION; do NEWAPPEND=$(date +%s) cp $file "$COPYDIR"/"$NEWAPPEND"$file rm $file done


0

I would do it like this: for f in [a-z][0-9].txt.gz; do mv $f "${f:0:1}0${f:1}" done I adapted this from something else (which used awk because it needed more, but thanks to a comment I made it better).


0

Based on Padding a number in a filename to a fixed length, I found this to work for me. This is a hacky way and I am surprised there is no proper/simple way to do this and without additional programs or functions. var=$(find . | grep -E "[a-z][0-9]{1}\.+" | sed 's/\.\///') for f in $var; do alpha=$(basename $f .gz | cut -c1-1) num=$(basename $f .gz |...


0

This would do it: find . -name '??.txt.gz' | sed -e 's/..\(.\)\(.*\)/mv \1\2 \10\2/' > /tmp/cmds sh /tmp/cmds The find command gets the list of files that need to be renamed. The sed command constructs a list of mv commands to do what you want. Those get written to a file and then executed as a shell script. I'm sure there's a way to do this as a one-...


2

The find command allows you to limit what files are matched. You can then call your script with the exec option e.g. find . \( -name '*.mp3' -o -name '*.avi' \) -exec /path/to/your/script.sh Now your script will be called for each mp3/avi file in the tree. A simple test to show this would be to have script.sh read something like #!/bin/bash echo ...



Top 50 recent answers are included