- Move Files Into One Directory in Linux With Find
- Move Files Into One Directory in Linux With Find
- Usage
- How to move file from one folder to different folder in linux
- You really should read Advanced Linux Programming and syscalls(2)
- Mv command in Linux with examples
- How to find a specific file and move it to a specific directory?
- 2 Answers 2
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
- How to move or copy files listed by ‘find’ command in unix?
- 5 Answers 5
Move Files Into One Directory in Linux With Find
Tail command in Linux with examples 21, Sep 17 wc command in Linux with examples 28, Sep 17 tAr command in Linux with examples 04, Oct 17 atrm command in Linux with examples 01, Mar 19 bc command in Linux with examples 09, Oct 17 AWK command in Unix/Linux with examples 10, Oct 17 tr command in Unix/Linux with examples 13, Oct 17 Paste command in Linux with examples 13, Nov 17 Comm command in Linux with examples 16, Nov 17 cmp Command in Linux with examples 16, Nov 17 cut command in Linux with examples 17, Nov 17 cp command in Linux with examples 24, Nov 17 uniq command in linux with examples 24, Nov 17 expand Command in LINUX with examples 27, Nov 17 date command in Linux with examples 01, Dec 17 df Command in Linux with examples 04, Dec 17 Article Contributed By : AKASH GUPTA 6 @AKASH GUPTA 6 Vote for difficulty Current difficulty : Basic Easy Normal Medium HardExpert Improved By : ankur035 Article Tags : linux-command Linux-directory-commands Linux-file-commands Linux-Unix Question: Suppose a particular command generates few files (I dont know the name of these files).
Move Files Into One Directory in Linux With Find
If you want to move all files of a particular file type into a single directory, you can use the find command to do this easily in Linux.
Usage
Run this command from the root directory of where you want to find the files. For instance, if you wanted to find all .zip files from any subdirectory under /home and move them into the /backup directory, you would use the following command:
find /home -iname '*.zip' -exec mv '<>' /backup/ \;
This would move all the files into the same directory, so any files that were duplicated would be overwritten. Note that the subdirectories would not be copied, just the files.
Move Files Into One Directory in Linux With Find, For instance, if you wanted to find all .zip files from any subdirectory under /home and move them into the /backup directory, you would use the following command: find /home -iname ‘*.zip’ -exec mv ‘<>‘ /backup/ \; This would move all the files into the same directory, so any files that were duplicated would …
How to move file from one folder to different folder in linux
How to move a particular file from one folder to another folder?
#include int main() < FILE *tFile; if (tFile != NULL) tFile = NULL; if ((tFile = fopen("TempFile.txt", "rw")) == NULL) < return -1; >mv("TempFile.txt", "../MST"); printf("Done Succesfully\n"); return 0; >
test.c:17:2: warning: no newline at end of file /tmp/ccKLWYNa.o(.text+0x5e): In function `main': : undefined reference to `mv' collect2: ld returned 1 exit status
Please guide me how can I do this.
You really should read Advanced Linux Programming and syscalls(2)
To move (from C) a file from one place to another in the same file system just use the rename(2) syscall.
At the very least, for your particular example, you’ll need to code:
char* srcpath = "TempFile.txt"; // assume it is a variable path char destpath[1024]; snprintf (destpath, sizeof(destpath), "../MST/%s", srcpath); if (rename (srcpath, destpath)) < // something went wrong if (errno == EXDEV) < // copy data and meta data >else < perror("rename"); exit(EXIT_FAILURE); >; > else < // the rename succeeded >
If you really want to mv TempFile.txt ../MST/TempFile.txt specifically for TempFile.txt only you could just call rename(«TempFile.txt», «../MST/TempFile.txt») and handle the error cases like I suggest. If you are sure that ../MST/ lie in the same file system than . then EXDEV should not happen and you don’t need to handle it particularly (but you do need to handle errors).
If you want to move a file between two different file systems, you have to copy the data (and perhaps some of the meta-data) yourself (and then remove e.g. with unlink(2)) the original source file). You could detect that situation by various means: you could just try the rename and if errno (see errno(3)) is EXDEV you need to copy the file. Or you could use stat(2) to query the source file(and the destination directory ) meta-data -e.g. its size and its file system.
Of course, you need to understand what are files on Linux (or Posix), in particular what is an inode. See inode(7) and credentials(7)
You could have used system with /bin/mv (but be careful about strange characters -like spaces or semicolons- in the file paths, you need to escape them to avoid code injection), apparently you don’t want to.
You should play with strace(1) (or perhaps also ltrace ) on mv in various situations to understand what it is doing. Also, study the source code of GNU coreutils which provides /bin/mv notably in mv.c .
Some extra C or C++ libraries may provide you with functions to move files (in the same filesystem they should do a rename , in different file systems they copy the source file data and perhaps some meta-data and unlink the source, so cannot be atomic), e.g. in C g_file_move (from Gio with Glib from Gnome), or in C++ copy_file -followed by remove in Boost, etc etc.
PS. For temporary files see tmpfile(3), mkstemp(3), etc.
How to move file from one folder to different folder in, If you want to move a file between two different file systems, you have to copy the data (and perhaps some of the meta-data) yourself (and then remove e.g. with unlink(2)) the original source file). You could detect that situation by various means: you could just try the rename and if errno (see errno(3) ) is … Code samplechar* srcpath = «TempFile.txt»;char destpath[1024];snprintf (destpath, sizeof(destpath), «../MST/%s», srcpath);if (rename (srcpath, destpath))
Mv command in Linux with examples
mv stands for move . mv is used to move one or more files or directories from one place to another in a file system like UNIX. It has two distinct functions:
(i) It renames a file or folder.
(ii) It moves a group of files to a different directory.
No additional space is consumed on a disk during renaming. This command normally works silently means no prompt for confirmation.
mv [Option] source destination
Let us consider 4 files having names a.txt, b.txt , and so on till d.txt .
To rename the file a.txt to geek.txt(not exist) :
$ ls a.txt b.txt c.txt d.txt $ mv a.txt geek.txt $ ls b.txt c.txt d.txt geek.txt
If the destination file doesn’t exist , it will be created. In the above command mv simply replaces the source filename in the directory with the destination filename(new name). If the destination file exist , then it will be overwrite and the source file will be deleted. By default, mv doesn’t prompt for overwriting the existing file, So be careful !!
Let’s try to understand with an example, moving geeks.txt to b.txt(exist) :
$ ls b.txt c.txt d.txt geek.txt $ cat geek.txt India $ cat b.txt geeksforgeeks $ mv geek.txt b.txt $ ls b.txt c.txt d.txt $ cat b.txt India
Options:
1. -i (Interactive): Like in cp, the -i option makes the command ask the user for confirmation before moving a file that would overwrite an existing file, you have to press y for confirm moving, any other key leaves the file as it is. This option doesn’t work if the file doesn’t exist, it simply rename it or move it to new location.
$ ls b.txt c.txt d.txt geek.txt $ cat geek.txt India $ cat b.txt geeksforgeeks $ mv -i geek.txt b.txt mv: overwrite 'b.txt'? y $ ls b.txt c.txt d.txt $ cat b.txt India
2. -f (Force): mv prompts for confirmation overwriting the destination file if a file is write-protected. The -f option overrides this minor protection and overwrites the destination file forcefully and deletes the source file.
$ ls b.txt c.txt d.txt geek.txt $ cat b.txt geeksforgeeks $ ls -l b.txt -r--r--r--+ 1 User User 13 Jan 9 13:37 b.txt $ mv geek.txt b.txt mv: replace 'b.txt', overriding mode 0444 (r--r--r--)? n $ ls b.txt c.txt d.txt geek.txt $ mv -f geek.txt b.txt $ ls b.txt c.txt d.txt $ cat b.txt India
3. -n (no-clobber): With -n option, mv prevent an existing file from being overwritten.
In the following example the effect is for nothing to happen as a file would be overwritten.
$ ls b.txt c.txt d.txt geek.txt $ cat b.txt geeksforgeeks $ mv -n geek.txt b.txt $ ls b.txt c.txt d.txt geek.txt $ cat b.txt geeksforgeeks
4. -b(backup): With this option, it is easier to take a backup of an existing file that will be overwritten as a result of the mv command. This will create a backup file with the tilde character(~) appended to it.
$ ls b.txt c.txt d.txt geek.txt $ mv -b geek.txt b.txt $ ls b.txt b.txt~ c.txt d.txt
5. –version: This option is used to display the version of mv which is currently running on your system.
$ mv --version mv (GNU coreutils) 8.26 Packaged by Cygwin (8.26-2) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Parker, David MacKenzie, and Jim Meyering.
How to find a specific file and move it to a specific directory?
How to find a specific file, and move it to the specific directory /var/tmp ? For example I want to find the file 0914_Jul-2014.gz . Remark, the file 0914_Jul-2014.gz , is under ~300 subdirectories:
find /usr -name '0914_Jul-2014.gz' -exec mv <> /var/tmp
2 Answers 2
find /usr -name '0914_Jul-2014.gz' -exec mv <> /var/tmp \;
Or for extremely nested directory hierarchies
find /usr -name '0914_Jul-2014.gz' -execdir mv <> /var/tmp \;
Although as the documentation states you must ensure that your $PATH environment variable does not reference the current directory (namely . ) if you use -execdir
@maihabunash, change -exec to -execdir and give it a shot. Before doing that make sure your PATH variable does not reference current directory (ie it does not contain . )
Use locate to find the file:
Then use mv to move the file
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to move or copy files listed by ‘find’ command in unix?
I have a list of certain files that I see using the command below, but how can I copy those files listed into another folder, say ~/test?
In regards to @EricJablow — you are correct. But also if you run -exec with a +; at the end of the statement it will copy in a single batch and if you use \; it will run a cp command for each file found. Cheers!
5 Answers 5
Adding to Eric Jablow’s answer, here is a possible solution (it worked for me — linux mint 14 /nadia)
find /path/to/search/ -type f -name "glob-to-find-files" | xargs cp -t /target/path/
It looks like xargs is more efficient than exec in situations where copying significant number of files.
Actually, you can process the find command output in a copy command in two ways:
- If the find command’s output doesn’t contain any space, i.e if the filename doesn’t contain a space in it, then you can use:
Syntax: find | xargs cp -t Example: find -mtime -1 -type f | xargs cp -t inner/
Syntax: find -exec cp '<>' \; Example find -mtime -1 -type f -exec cp '<>' inner/ \;
In the second example, the last part, the semi-colon is also considered as part of the find command, and should be escaped before pressing Enter . Otherwise you will get an error something like:
find: missing argument to `-exec'