Tag Info

New answers tagged

4

Don't worry, you're not the first and won't be the last one to be bitten by that. It's the most frequently asked question about shells, and that's responsible for most bugs and security vulnerabilities found in shell scripts. That's due to a bad design choice of the Bourne shell and the Korn shell in the earlier Unix history. In the Bourne shell and most of ...


4

You must quote the whole file name: mv /sample/pdf/noriginalName.pdf "/sample/outputPdf/${NAME}${Name1}.pdf" Using double quote prevent bash and other Bourne-like shells (except zsh) perform Field splitting after your variable is expanded.


0

I tried all the scripts but none of them worked for some reason, or only partially worked. I found this bash script and tweaked it to look for .jpg files instead of .xml files: #!/bin/bash c=1; d=1; mkdir -p dir_${d} for jpg_file in *.jpg do if [ $c -eq 501 ] then d=$(( d + 1 )); c=0; mkdir -p dir_${d} fi mv ...


0

This will list files with ls *.jpg, take first 3000 with head -n 3000, make directory for them with another name defined in $FOLDERLIST and move files into it, this loop repeats 10 times LISTFILESCMD='ls *.jpg' FQUANTITY=3000 FOLDERLIST=`seq -w 1 10` for FOLDER in $FOLDERLIST; do mkdir $FOLDER; mv `$LISTFILESCMD | head -n $FQUANTITY` $FOLDER; done u may ...


1

How about the following: for letter in {A..Z}; do dir=/path/to/sorted/directories/$letter mkdir $dir mv "${letter}*.jpg" $dir/ done And then of course the same loop with the lower case letters {a..z}, except that in that case you don't want to create a separate lower case directory. Then the move line changes to: mv "${letter}*.jpg" ${dir^^}/ ...


0

Assuming you have one directory which you want to move files out of. You can use: $ mv <source folder/*.jpg <destination folder/> Edit: The script below finds all of the files that match the searching criteria defined by file_screen, then executes a while loop to go move the selected files. New directories are created based on modulo ...


0

I think something like: #!/bin/bash shopt -s nullglob for i in <directory>; do mv *.jpg "$dir" done This will move all jpegs to $dir which you will need to set or you will need to make possibly create an array with the directory name(s). You can easily customize this code by switching *.jpg to any different format. You will need to tweak around ...


0

let's say your files are scattered in various directory, Assuming that dirx is your parent directory, this can do the job using find: for f in `find /dirx -iname '*.txt' -type f -print`;do mv "$f" ${f%.txt}.text; done


7

With zsh: autoload zmv zmv '(**/)(*.*)' '$1${2//./_}' Otherwise, if you have access to bash (though ksh93 or zsh will do as well), you could always do: find . -depth ! -name '.*' -name '*.*' -exec bash -c ' for file do dir=${file%/*} base=${file##*/} mv -i -- "$file" "$dir/${base//./_}" done' sh {} + (though you'll miss the sanity ...


3

The problem is you're moving a file onto a directory. This is allowed to fail. I'm going to tell you how it once was. mkdir used to essentially read this (while I'm writing this in sh, it was really written in C and setuid-root). mknod d $1 ln -d $1 $1/. ln -d `dirname $1` $1/.. So as you can see there's not much special about . and .. except for the ...


16

.. is not special, it is just that it already exists. On Unix, Dos and MS-Windows every directory has a directory . it links back to itself, and a directory .. it links to its parent directory (or self if root directory). If .. and . are special it is only because you can not remove them (actually you can, you just remove the directory that contains ...


45

You can't rename a file to . or .. because all directories already contain entries for those two names. (Those entries point to directories, and you can't rename a file to a directory.) mv detects the case where the destination is an existing directory, and interprets it as a request to move the file into that directory (using its current name). ...


0

Short answer, set this setting in your config: "atomic_save": false It will avoid renaming over sshfs (SFTP), which is not supported.


1

if you have perl, you usually have rename. you can do: > type rename rename is /usr/bin/rename and show how this script is written: > cat /usr/bin/rename | head -n 5 #firt 5 lines for example #!/usr/bin/perl -w # # This script was developed by Robin Barker (Robin.Barker@npl.co.uk), # from Larry Wall's original script eg/rename from the perl ...


4

Not counting mv, you don't really need an outside process for this at all - you can kind of just poof them. ifsqz() ( LC_ALL=C sqz=$1 isf() { [ -e "$1" ] || [ -L "$1" ] ; } set -- * ; set -f for f do isf "$f" || break IFS=$sqz; set -- $f; IFS= isf "$*" || mv -- "$f" "$*" done ) Still though, that means a mv invocation per file, ...


5

I would replace all those tr commands, with a sed substitution command, e.g.: for file in %N; do mv "$file" "$(echo "$file" | sed 's/[ _-]//g')" done


7

The version of rename that comes with the perl package supports regular expressions: rename "s/[-_ ]//g" * Alternatively, rename -i "s/[-_ ]//g" * The -i flag will make rename use interactive mode, prompting if the target already exists, instead of silently overwriting.


0

You need to give each an unique name, as follows: Assuming one of your external partitions is /dev/sdb1 - you'll need to alter it for your setup and carry this out on all offending partitions/filesystems. lsblk will show you all of them. Check whether Ubuntu is showing you the partition or filesystem label: blkid /dev/sdb1 /dev/block/253:1: ...


1

There are two unrelated programs called rename. The one found on Debian and derivatives (Ubuntu, Mint, …) is a Perl script, and its first argument is a Perl expression that transforms the old name into the new name. With that script, you can do rename 's/.*-lib/NORMAL_X-lib/' NORMAL_*-lib* The s operator performs a regular expression replacement. There ...


1

If you are lucky enough to have rename available, then the following should be sufficient: rename 's/(NORMAL).*(-lib)/$1X$2/' *


2

The useful Perl powered rename isn't always available on all distros. For example, Fedora and CentOS (and presumably RedHat) use a basic rename utility that does basic search and replace and nothing much else. If you're unfortunate enough to be using one of those, then something like the following may help: for oldname in *; do newname=$(echo ...


0

You could consider using lookarounds, like (?<=NORMAL).*?(?=-lib) i.e. $ rename -v -n -- 's/(?<=NORMAL).*?(?=-lib)/X/' * NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.insertion renamed as NORMALX-lib4.insertion NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.inversion renamed as NORMALX-lib4.inversion NORMAL_H_LS-A7-A0CE-10A-01D-A017-09-lib4.translation renamed ...


0

while read tname fname do mv ${fname} ${tname} done < filelist.txt


1

No awk or sed necessary, since your filelist.txt is already in the perfect format. A few things in advance… I presume that there is no whitespace in the file names, and that a space separates the two filenames on each line. Also, I assume that filelist.txt terminates with a EOL before the EOF. Of course, make a backup of the directory before running this ...


0

As an alternative to the other answers, you can use the graphical tool GPRename. It can replace characters, truncate filenames, etc. The advantage is that there's a built-in preview function to check the new filenames before renaming them. But since it works on one directory at a time, it will be inconvenient to use it with numerous folders in sub-folders. ...


6

With the Perl rename tool (which is called rename on Debian and friends including Ubuntu, it may be prename elsewhere): rename -n 's/(?<!\.)jpg$/.jpg/' * # -n makes it show you what it'll do, # but not actually do it. Remove the -n to # actually rename To break down that patter: the ...


3

While sed is a very useful and versatile tool, you're not using it properly. It's best used to match and substitute strings in text files; it can't directly rename files on the filesystem. This task is better suited for a bash one-liner (assuming that's your shell). To rename something like . filejpg to file.jpg, use this: find . -name '. *' -print0 | ...


3

You need to quote file name: rm 'my file' my_file or escape space character: rm my\ file my_file


3

You have to quote or escape the filename. In Bash (the default shell in most distros), you can either use quote marks to enclose the entire name, or a backslash to escape the one space. rm "my file" rm my\ file



Top 50 recent answers are included