Tag Info

Hot answers tagged

45

Zsh mv Foo/*(DN) Bar/ or setopt -s glob_dots mv Foo/*(N) Bar/ (Leave out the (N) if you know the directory is not empty.) Bash shopt -s dotglob nullglob mv Foo/* Bar/ Ksh93 If you know the directory is not empty: FIGNORE='.?(.)' mv Foo/* Bar/ Standard (POSIX) sh for x in Foo/* Foo/.[!.]* Foo/..?*; do if [ -e "$x" ]; then mv -- "$x" Bar/ done ...


38

Use brace expansion: mv very/long/path/to/filename.{old,new} would expand to mv very/long/path/to/filename.old very/long/path/to/filename.new


29

I don't like the idea to overwrite binaries from coreutil when there are simpler solutions, so here are mine: rsync: Rsync copies files and has a -P switch for a progress bar. So if you have rsync installed, you could use a simple alias in your shells dotfile: alias cp='rsync -aP' The downside is, that rsync is a little bit slower than cp, but you should ...


26

rsync would probably be a better option here. It's as simple as rsync -a subdir/ ./. My test tree in filename:contents format: ./file1:root ./file2:root ./dir/file3:dir ./dir/file4:dir ./subdir/dir/file3:subdir ./subdir/file1:subdir Running rsync: $ rsync -a -v subdir/ ./ sending incremental file list ./ file1 dir/ dir/file3 Gives: ./file1:subdir ...


23

If the last argument was a directory, you just moved all of the files and directories in your current working directory (except those whose names begin with dots) into that directory. If there were two files, the first file may have overwritten the second file. Here are some demonstrations: More than two files and the last argument is a file $ mkdir d1 d2 ...


22

A directory is (conceptually) a special "file" which contains a list of names, and the inode numbers those names point to. Some of names can be subdirectories. There is a special entry .. which points to the parent directory. So, its clear, changing the name of a file is easy: you just change the name in the directory entry, nothing else. This holds whether ...


21

You can build a patched cp and mv which then both support the -g switch to show progress. There are instructions and patches at this page. However: The page instructs you to do $ sudo cp src/cp /usr/bin/cp $ sudo cp src/mv /usr/bin/mv which overwrites the original cp and mv. This has two disadvantages: Firstly, if an updated coreutils package arrives at ...


20

If you have rsync (remove --dry-run to do it for real): rsync --dry-run --remove-source-files -avHAX /unencrypted/ /encrypted Otherwise, using bash4+ and GNU stat: #!/bin/bash set -e shopt -s nullglob globstar for from in /unencrypted/**/*; do to="${from/\/un//}" if [[ -d "$from" ]]; then echo mkdir -p "$to" echo chmod "$(stat ...


18

With the folder called 'myfolder' and up one level in the file hierarchy (the point you want it to put) the command would be: mv myfolder/* . So for example if the data was in /home/myuser/myfolder then from /home/myuser/ run the command.


17

This should work: mkdir pretty_name && tar xf ugly_name.tar -C pretty_name --strip-components 1 -C changes to the specified directory before unpacking (or packing). --strip-components removes the specified number of directories from the filenames stored in the archive. Note that this is not really portable. GNU tar and at least some of the BSD ...


16

You can use the advanced globbing patterns in some shells to match all the files in a directory except for those matching a particular pattern. For example, in ksh, bash or zsh, the command shopt -s extglob ## needed in bash only setopt ksh_glob ## needed in zsh only mv /source/!(*.bak) /destination will move all files in /source to /destination ...


16

Programs connect to files through a number maintained by the filesystem (called an inode on traditional unix filesystems), to which the name is just a reference (and possibly not a unique reference at that). So several things to be aware of: Moving a file using mv does not change that underling number unless you move it across filesystems (which is ...


15

If you need to rename files in subdirectories as well, then you can do find /search/path -depth -name '* *' \ -execdir bash -c 'mv "$1" "${1// /_}"' _ {} \; Thank to @glenn jackman for suggesting -depth option for find and to make me think.


15

In Unix, almost everything is a file. A directory is a special type of file that from the user's perspective can "contain" other files. The error Not a directory occurs because your existing file is not a directory, and since a directory is a type of file, and there cannot be two identically named files in one directory, the operation cannot be performed.


15

rsync -va -n /oldisk/a/ /newdisk/a/ The -n will do a dry run, showing you what it would do without actually doing anything. If it looks ok, run the rsync without the -n option. This will be a copy, not a move, which isn't quite what you're doing, but is safer.


15

The good old perl rename: rename 's/(\d+)(\.jpg)/($1-1).$2/e' * [Remarks] Image numbers should be greater than 0. In case images are greater than 9 and have not leading 0s, use $(ls -v1 *) to avoid clobbering. Proposed by @arielf and noticed by @Graeme. When in doubt use also -v for verbose and -n for no-action.


14

I guess you see this � invalid character because the name contains a byte sequence that isn't valid UTF-8. File names on typical unix filesystems (including yours) are byte strings, and it's up to applications to decide on what encoding to use. Nowadays, there is a trend to use UTF-8, but it's not universal, especially in locales that could never live with ...


13

Don't try to parse find output except as a last resort. It is important to realize that on Unix file systems, file names are not strings (a common misconception) but rather binary blobs which can contain any character except / and the null character. Parsing file names safely and correctly is enough of a pain that 99% of the time you'll just want to avoid ...


13

First the shell expands ./* to all files in the current directory (except files starting with a dot). if there is only one file: mv fails if there are two file: the first one is moved to the second (which therefore get lost) if there are more than two files: if the last one is a directory: all files are moved into this directory otherwise mv fails.


12

This has little to do with xterm. You could do the same thing with two shells without invoking xterm at all. For that matter, you could do it with one shell (see below). Each process has a current working directory. This isn't tracked by name, but as a pointer (more or less) to the directory itself. (I'm not sure how it's represented internally; it may be ...


12

This is entirely dependent of if there was already a directory named '/hello2' already in existence or not. If /hello2 exists AND is a directory, then move will always move /hello to /hello/hello2 If /hello2 does not exist, then move will always rename /hello to /hello2 If /hello2 exists AND is a file, you will get an error, "cannot overwrite ...


11

With GNU tar ≥1.16, use --transform to apply a sed regexp transformation to each file name (the transformation is applied on the full path in the archive): tar xf foo.tar --transform 's!^ugly_name\($\|/\)!pretty_name\1!' If ugly_name is the toplevel component of all the file names in the archive, you don't need to bother with precise matching: tar xf ...


11

Under ksh, bash or zsh: svn mv !(2010) 2010 Under bash, you need to run shopt -s extglob first (put it in your ~/.bashrc). Under zsh, you need to run setopt -o ksh_glob first (put it in your ~/.zshrc). This doesn't move dot files (files whose name begins with .). If you have some, move them separately. Take care to exclude the .svn directory if you have ...


11

Moving a file or directory changes the meta-data property that identifies its parent in the file tree, but it doesn't change its actual node id. On the physical disk it's still in the same place, and the filesystem still knows it as the same object. Anywhere the file or directory pointer is open, it is already connected to that object, and a change to the ...


11

Bash or Ksh together with mv could solve it: for f in *.png; do mv -n "$f" "${f/-0}"; done In case the file name may have “0” after the first dash too and the “-0” is always in front of the dot, you may want to include that dot too in the expression: for f in *.png; do mv -n "$f" "${f/-0./.}"; done But as that renaming rule is simple, if you have ...


11

Move (mv) is essentially an attribute-preserving copy followed by a deletion (rm), as far as permissions are concerned.1 Unlinking or removing a file means removing its directory entry from its containing directory. You are writing to the directory, not the file itself, hence no write permissions are necessary on the file. Most systems support the ...


11

for file in * ; do echo mv -v "$file" "${file#*_}" done run this to satisfy that everything is ok. if it is, remove echo from command and it will rename files as you want. "${file#*_}" is a usual substitution feature in the shell. It removes all chars before the first _ symbol (including the symbol itself). For more details look here.


11

You can do this with GNU find and GNU mv: find /dir1 -mindepth 2 -type f -exec mv -t /dir1 -i '{}' + Basically, the way that works if that find goes through the entire directory tree and for each file (-type f) that is not in the top-level directory (-mindepth 2), it runs a mv to move it to the directory you want (-exec mv … +). The -t argument to mv lets ...


10

Well on Linux you could use inotify to track changes to your files. Inotify is in-kernel and has bindings to many different languages allowing you to quickly script such functionality if the app you are working with does not support inotify yet.


10

rsync can delete the source after copies with the --remove-source-files parameter. This should be a convenient way to do what you'd like. From the rsync man page: --remove-source-files sender removes synchronized files (non-dir)



Only top voted, non community-wiki answers of a minimum length are eligible