Tag Info

Hot answers tagged

51

Try this (works with csh, tcsh, ksh, zsh, bash or fish): mv ~/folder/subfolder/file.{txt,sh}


41

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

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 ...


26

You will have to copy them to the destination and then delete the source, using the commands cp -r * .. followed by rm -rf *. I don't think you can "merge" directories using mv.


25

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 ...


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

If you are using bash: for f in *.png; do mv "$f" "${f#image}"; done


21

Use rsync(1): rsync \ --remove-source-files \ --chown=unicorn:unicorn \ /home/poney/folderfulloffiles /home/unicorn/


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 ...


19

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 ...


19

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.


18

This has worked. Perl's rename: rename -v 's/image//' *.png


18

With bash, you could use brace expansion mv blob/a_long_directory_name/{c/x.x,evenmore/y.y}


17

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.


17

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.


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

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

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

zmv The zsh shell has a powerful batch rename command called zmv. First you need to enable the zmv command as follows (this can go into your ~/.zshrc). autoload zmv The basic syntax is zmv PATTERN REPLACEMENT. The pattern is a shell glob expression. Parts of the pattern can be surrounded by parentheses. The replacement text can contain $1, $2, etc. to ...


13

Just found out here that jhead can do it all for you! :) jhead -autorot -nf%Y-%m-%d_%H-%M-%S *.jpg


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

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)


12

Check if /home/chankey/Desktop/Download for some reason would be a symlink to for example /home/chankey/Download ... If so, it may be that that the file is in /home/chankey/


12

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 ...


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

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 ...


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 ...



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