date int64 1,220B 1,719B | question_description stringlengths 28 29.9k | accepted_answer stringlengths 12 26.4k | question_title stringlengths 14 159 |
|---|---|---|---|
1,336,040,124,000 |
I am trying to copy a file from Root directory over to my home directory. I had to execute the extraction of a file as ROOT user. I can see it is extracted into root directory but I now want to access this in my home directory. I have tried a number of commands to do this but it seems to fail.
|
Assuming your username is deirdre:
As root, you have to move the file from root's homedir to deirdre's homedir and change its owner to deirdre:
mv /root/somefile ~deirdre/
chown deirdre: ~deirdre/somefile
Once you've done this, you will be able to login as deirdre and access the file.
It isn't clear from your questio... | Copying a file from ROOT to home directory |
1,336,040,124,000 |
I'd like to use cp -b to copy a file to a destination, possibly creating a backup file of the destination path if it already exists. But, if the backup file already exists, I'd like to have cp fail with an error.
I know I can use -n to avoid clobbering the target file, but I want to instead refuse to clobber the backu... |
If you want to avoid clobbering any backup files with GNU cp, you can use numbered backups:
cp --backup=t source destination
Rather than overwrite a backup, this creates additional backups.
Example
As an example, let's consider a directory with two files:
$ ls
file1 file2
Now, let's copy file1 over file2:
$ cp --b... | Can I use the 'cp' command with the -b option, but have it fail if the backup file already exists? |
1,336,040,124,000 |
I have made my peace with the fact that NTFS(-3g) on Linux will be slower than NTFS performance on Windows. I can write to my external NTFS-formatted USB 3.0 HDD at about 100+ MB/s on Windows while I have to settle for 30 MB/s (give or take) on my Debian (Wheezy) box.
That's not the problem, however. I found (empirica... |
You are seeing the effects of drive head seek latency when running the parallel copies.
With most file systems, including NTFS and ext[234], data is stored in distinct locations on the drive. File system information here, block allocation data over there and file data way over there.
When writing a single file, the me... | Copying to an external NTFS partition: slows down when I copy many files at once |
1,336,040,124,000 |
I don’t want the “last modified” attribute to be changed to the current date when copying files to a mounted Samba folder. How can I do this?
This behavior occurs with (K)Ubuntu 12.04 and Ubuntu 15.10. It can be reproduced using GUI browsers (tested with nautilus 3.4.2 and dolphin 2.0) and using cp -p in terminal.
T... |
The command cp -P doesn't apply to your needs. You are using uppercase argument letter -P which is used to never follow symbolic links. What you want to use is lowercase to preserve timestamps: cp -p
As described in the comment section of the question, using the correct gid and uid solved the problem:
sudo mount -t... | Prevent updates to 'modified time' when copying files to a mounted Samba folder |
1,336,040,124,000 |
In my first question: How do I get the creation date of a file on an NTFS logical volume, I asked how to get the "Date created" field in NTFS-3G. Now, that I know I can get the "Date created", I have started adding files onto my NTFS-3G partition and would like to set the "Date created" of each file to its "Date modif... |
The system.ntfs_times extended attribute contains 32 bytes that consist of the btime, mtime, atime, ctime as 64bit integers.
You can list them with for instance:
getfattr --only-values -n system.ntfs_times -- "$file" |
perl -MPOSIX -0777 -ne 'print ctime $_/10000000-11644473600 for unpack("Q4",$_)'
So you can just ... | How do I recursively set the date created attribute to the date modified attribute on NTFS-3G? |
1,336,040,124,000 |
I am searching files who have either been created or modified for the last 60 minuts. I find these via
find ~/data/ -cmin -60 -mmin -60 -type f
~ the home directory /usr/wg/
After that I want to copy these files and preserve the main folder structure...
The results of the find command are for instance...
/usr/wg/data/... |
You should rewrite your command on this way:
cd ~/data; find . -cmin -60 -mmin -60 -type f
to be able to get from find relative paths
And maybe something like
find ... -exec cp -r "{}" ../vee/ \;
will do the work with copy the files with subdirectory structure
| Finding files and copy with folder structure intact |
1,336,040,124,000 |
In directory /source I'd like to copy all files with a file name more than 15 characters to directory /dest. Is there a UNIX command to do this?
EDIT: Although this question explains how to search for a filename of a certain length, my question also asks how to copy the file.
|
You can make a pattern with 16-or-more characters and copy those files.
A simple (but not elegant) approach, using 16 ? characters, each matching any single character:
for n in /source/????????????????*;do [ -f "$n" ] && cp "$n" /dest/; done
After the 16th ?, use * to match any number of characters. The pattern mi... | Copy All Files With Certain Length File Name |
1,336,040,124,000 |
Can we set permissions, on a Linux box OR for a particular directory (Scientific Linux in my case) that, one can read files but cannot copy, move or delete them?
Update: My Scenario is,
We developed an GUI program which are having some images which we have created by consuming lot of time and efforts. Our directory s... |
If a file can be read, it can be copied. You can, however, stop the file from being deleted or moved, by not giving write permissions to the directory where the file resides.
Edited with additional info since the question has been amended:
Given the scenario you've now added to the question, you could do this:
creat... | How do I disable copy permissions? |
1,336,040,124,000 |
When using rsync to copy files over the network, I give a path so that rsync will know where to put the file on the remote server.
rsync -av /home/ME/myfile user@remoteserver:/home/ME/
If I leave off the remote path, where will rsync put the file? Eg:
rsync -av /home/ME/myfile user@remoteserver
|
rsync -av /home/ME/myfile user@remoteserver
This command will not send the file to your remote server, it will just make a duplicate of the /home/me/myfile in your current working directory and the name of the file will be called user@remoteserver.
Just like when you want to create a backup of a file before editing i... | Where does rsync copy a file if I don't specify the remote path? |
1,336,040,124,000 |
I have the following folder and files:
.
├── photos
│ ├── photo-a
│ │ ├── photo-a.meta.json
│ │ └── photo-a.json
│ ├── photo-b
│ │ ├── photo-b.meta.json
│ │ └── photo-b.json
...
There are more folders and files in the photos folder in the same structure
I would want to copy all the files photo-a.... |
A couple of options spring to mind.
rsync --dry-run -av --prune-empty-dirs --exclude '*.meta.json' --include '*.json' photos/ photos-copy/
Or if you don't have rsync (and why not!?), this will copy the files retaining the structure
cp -av photos/ photos-copy/
rm -f photos-copy/*/*.meta.json
This variant will flatten... | How can I copy all files while excluding files with a certain pattern? |
1,336,040,124,000 |
I want to copy an entire file structure (with thousands of files and hundreds of directories), it's a hierarchy of directories and there are those node_modules directory that I want to exclude from the copying process.
Is there a Unix command to copy from a directory and all of its files and sub-directories recursivel... |
You can try with rsync or tar command .
See this or this post.
From rsync man page
--exclude=PATTERN exclude files matching PATTERN
--exclude-from=FILE read exclude patterns from FILE
rsync -avz --exclude 'dir*' source/ destination/
| Take all the file structure but these directories |
1,336,040,124,000 |
I have a folder root_folder containing a lot of subfolders. Each of these subfolders contains a small number of files (between 1 and 20) and I want to copy all the subfolders containing at least 5 files into another folder new_folder. I have found how to print the folders that interest me: https://superuser.com/questi... |
You can do a for loop on the find result and copy the folder with -R :
IFS=$'\n'
for source_folder in "$(find . -maxdepth 1 -type d -exec bash -c "echo -ne '{}\t'; ls '{}' | wc -l" \; |
awk -F"\t" '$NF>=5{print $1}');" do
if [[ "$source_folder" != "." ]]; then
cp -R "$source_folder" /destination/folder
fi
... | Copy subfolders containing at least n files |
1,336,040,124,000 |
I have a folder with a lot of files. I want to copy all files which begin with of these names (separated by space):
abc abd aer ab-x ate
to another folder. How can I do that?
|
With csh, tcsh, ksh93, bash, fish or zsh -o cshnullglob, you can use brace expansion and globbing to do that (-- is not needed for these filenames, but I assume they are just examples):
cp -- {abc,abd,aer,ab-x,ate}* dest/
If you'd rather not use brace expansion, you can use a for loop (here POSIX/Bourne style syntax)... | copying files with particular names to another folder |
1,336,040,124,000 |
How can I compare all files in two directories and copy the ones that are different to another directory? For example, say we have dir1 and dir2:
dir1:
build.gradle
gradle.properties
somejar.jar
javacode.java
anotherjar.jar
dir2:
build.gradle <-- different from build.gradle in dir1
... |
All you need is
cp -n dir2/* dir1/* dir3/
| Comparing each file in two directories and copying it to another if it differs from its counterpart |
1,336,040,124,000 |
I have a statistical application which runs every minute and creates charts accordingly.
In order to make these charts available to other users, I need to copy the whole folder containing the charts and paste it to a shared folder where other users can see the contents.
How can I automate this process so that e.g each... |
This sounds like something which could perhaps be perfectly solved with rsync. In its simplest form it can be called like this
rsync sourceFolder destinationFolder
Called in a crontab every 5 minute:
*/5 * * * * /usr/bin/rsync sourceFolder destinationFolder
For options, permissions, exlude of special files or direct... | How can I automate the process of copying files from one folder to another in Centos |
1,336,040,124,000 |
I have the string xyz which is a line in file1.txt, I want to copy all the lines after xyz in file1.txt to a new file file2.txt. How can I achieve this?
I know about cat command. But how to specify the starting line?
|
Using GNU sed
To copy all lines after xyz, try:
sed '0,/xyz/d' file1.txt >file2.txt
1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.
Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but t... | How to copy the rest of lines of a file to another file [duplicate] |
1,336,040,124,000 |
I have a directory that have zillions of files. As soon as I try to copy this directory, I receive a message that a file could not be copied (corrupt ?) and the copy stops.
Is there any command I can type on terminal that can check all files on that directory tree and list all files that could not be copied or that a... |
rsync can be used to copy directories, and is capable of restarting the copy from the point at which it terminated if any error causes the rsync to die.
Using rsync's --dry-run option you can see what would be copied without actually copying anything. The --stats and --progress options would also be useful. and --h... | Finding corrupted files |
1,336,040,124,000 |
I am looping through a number of files, and $file represents the current file.
How can I make copies or renames and keep the extension the same for this current file?
e.g. if $file = x.jpg
How to make a copy of $file's with a filename of x_orig.jpg
So far I have:
for file in /tmp/p/DSC*.JPG; do
cp $file $file+'_orig... |
You can use bash's string substitution features for that:
for file in /tmp/p/DSC*.JPG; do
cp "$file" "${file%.JPG}"_orig.JPG
done
The general format is ${string%substring} which will remove substring from the end of string. For example:
$ f=foobar.JPG; echo "${f%.JPG}"
foobar
| How to copy multiple files but keep their extensions the same? |
1,336,040,124,000 |
I am having to put together a script that will ssh into devices to run a command such as "show running-config" and save the output to a file on my local machine. I have done similar tasks like this right from the command line and have it save the file to my local system. For example,
ssh [email protected] ls > ls_fro... |
The expect command you use:
spawn ssh [email protected] ls > ls_from_remotes_sys
This, effectively calls
exec("ssh","[email protected]","ls",">","ls_from_remotes_sys")
That means the three parameters (ls, > and the filename) are sent to the remote system; ie the redirection happens on the remote system.
A kludge cou... | Capture stdout from ssh session to local machine |
1,336,040,124,000 |
There is a collection of .doc files, in addition to other types of files, on a remote server (which supports SCP).
I am trying to write a script to retrieve the latest (most recently modified) .doc file from the remote server. The path to my current working directory cannot be absolute since my script may be deployed... |
I am not really clear what your problem is, but if you're trying to copy to the current directory then just use . to refer to the current directory so that your command is:
scp -i key.pem abc@xyz:/tmp/*.doc .
| Copying latest file from remote server |
1,336,040,124,000 |
I just wanted to confirm this behaviour. If I copy something in Midnight Commander with the option to put it in the background and then I start a second process of copying something in that same Midnight Commander console, does this break the first process? I have the feeling it does.
And does somebody know how to get... |
I think I found out what happened. Midnight Commander can handle several background processes. But it might stop them if Midnight Commander is exited. It will, however, resume on restart.
| Does Midnight Commander cancel a background copy if another background copy is initiated? |
1,336,040,124,000 |
I often transfer large files from a remote server using rsync with the following command :
rsync -rvhm \
--progress \
--size-only \
--stats \
--partial-dir=.rsync-partial \
"user@server::module/some_path" "some_path"
That way, even if the transfer fails, I can resume it later and I know that I'll ... |
It doesn't look like this is possible yet although there is a patch available allowing the use of the --inplace option in conjunction with --partial-dir to avoid this copy.
Refer to Bug 13071 for further details but from the description:
If --inplace is used with --partial-dir, or with any option implying it (--delay... | rsync keeps previous partial file when resuming |
1,336,040,124,000 |
I have a notebook which can't boot up Ubuntu and I want to copy all of the files to an external hard drive with USB connection to save them. After I want to install a new Ubuntu to the notebook, how can I copy all of the files to an external hard drive using just the grub console, or (initramfs) console?
Update:
sudo ... |
After I want to install a new Ubuntu to the notebook, how can I copy...
I think you mean first copy the files to an external drive, then reinstall Ubuntu.
You can't copy files with the GRUB console: the filesystem drivers of GRUB are basically read-only. (You can write into e.g. the /boot/grub/grubenv file, but only... | How to copy files with grub console? |
1,425,620,266,000 |
I get all the modified svn files using svn st | grep ^M command
M student/includes/class_student_promotion.php
M student/includes/class_student_report.php
M student/resources/js/student_co_scholistic_activities.js
M staff/php/edit_staff_details.php
M library/includes/class_book_return.php... |
Assuming that with folder you mean directory and assuming you have no spaces or special characters in your file and directory names:
svn st | ack '^M' | cut -b 8- | cpio -pdmv backup
This is cpio in pass-through mode (-p). It takes a list of filenames to copy from stdin. -d allows it to create directories, -m preserv... | Find all the modified svn files and copy files modified in the same folder structure |
1,425,620,266,000 |
Today I found a little bug in my own written CMS.
Now I want to rewrite all the files on the server with the same filename.
However, I do have some customers with the specific file customized so I can't overwrite all of the files.
Since some customers have a customized file, I need to check the filesize.
When the file... |
I assume you changed the source code in one of your source files on one of your servers and want to port the fix onto other web sites, right? If you kept a copy of the orginial file (I always do when I don't know the project by heart), use your best asset: diff:
diff -Nau old/file new/file > file.patch
You will have ... | Find file, check size and overwrite when filesize is different |
1,425,620,266,000 |
If process A copies files to some location loc and process B regularly copies the files from loc to some other location, can B read a file that is currently in the process of being copied to loc by A?
I'm using Ubuntu Linux 12.04 if that's important.
Background information: I want to continuously backup a PostgreSQL ... |
I think the best thing to is to ensure that process B only copies files which have been fully transferred by process A. One way to do this would be to use a combination of cp and mv in process A, since the mv process uses the rename system call (provided the files are on the same filesystem) which is atomic. This mean... | Can I safely read a file that is appended to by another process? |
1,425,620,266,000 |
I have 3 distinct folders: history, inbox, backup.
I need to copy all the files from 'history' to 'inbox', only if they are not present in 'backup'.
How to do this?
|
Just an example, is there any subfolder in history ?
for x in history/*;
do
[[ -f backup/"$(basename "$x")" ]] || cp "$x" inbox
done
This script would loop through all possible files in history folder, and extract the basename of it (e.g the basename of /bin/ls is ls), and check if the file exists in backup folder;... | Advanced file filtering |
1,425,620,266,000 |
Where do I find documentation of behavior of cp and rsync commands when the destination path shares the inode with another path? In other words, when I do
$ cp [options] src dest
$ rsync [options] src dest
and when there is dest2 that is a hard link to the same inode as dest, do these commands modify the content at ... |
cp’s behaviour is specificied by POSIX. -a isn’t specified by POSIX, but it implies -R which is. When copying a single file, without -R, and the target exists,
A file descriptor for dest_file shall be obtained by performing actions equivalent to the open() function defined in the System Interfaces volume of POSIX.1-2... | Hard link as destination of cp and rsync |
1,425,620,266,000 |
I have a question related to the serial terminal. It is sometimes possible to connect to a device using a command such as screen. One example would be screen /dev/ttyUSB0 115200.
I can connect through a Linux ARM device with it (even passing the login phase). Thus, I can easily transfer everything that is text. Now, I... |
Instead of using screen, you might want to use a dedicated serial terminal emulator program, such as minicom, since it has built-in support for the local side of serial-port binary transfer protocols like ZMODEM.
To transfer a file from local system to an ARM device, you would need to have the command-line tool for t... | Send a binary file through serial terminal |
1,425,620,266,000 |
What I have done is clone a small 32GB flash module that had three partitions. I happened to have a 32GB USB lying around, so I thought it might just work; it did not. It seems 32GB from Toshiba is a bit different than 32GB from Sandisk.
Anyway, so then took to a 2TB external drive and did the exact same thing. Speci... |
Just copy the partitions that you need and the MBR if you need it too.
The MBR is stored in the the first 512 bytes of the disk.
dd if=/dev/sdX of=/path/to/mbr_file.img bs=512 count=1
Copy each partition
dd if=/dev/sdX1 of=/path/to/partition1.img bs=512
dd if=/dev/sdX2 of=/path/to/partition2.img bs=512
dd if=/dev/s... | How to clone an entire disk to a larger disk and then offload? |
1,425,620,266,000 |
We are using rsync to sync two folders on same machine.
Files will be written to a source folder from another application. We have the problem that, even if a file is not completely written/copied to the source folder, rsync copies that file to destination.
Is there any way/option to check/transfer only complete files... |
If the sizes of the files are fixed (after the write operation of the application), you can transfer only the files based on the size so the files that are not done being written to yet will not be copied :
--max-size=SIZE don't transfer any file larger than SIZE
--min-size=SIZE don't transfer any file... | rsync option to exclude partial files |
1,425,620,266,000 |
Copy files to a destination folder only if the files already exist. but the source files have a different file extension.
i.e
I have a backup of some files with ".desktop.in" extension and I want to copy to destination where the files extensions are ".desktop" and only the files that already exist in the destination.
... |
Found two ways:
for file in /src/*.desktop.in; do
file=${file%.in}
if test -e "/dest/$(basename ${file})"
then cp "/src/${file}.in" "/dest/${file}"
fi
done
rsync and --existing:
for file in /src/*.desktop.in; do
rsync --dry-run --existing --verbose "/src/${file}" "/dest/${file%.in}"
done
| Copy files to a destination folder only if the files already exist. but the source files have a different file extension |
1,425,620,266,000 |
I have files with random numbers in /home/user/files folder (every 4 days I have new ones).
for example:
john20
john25
john40
tom12
tom32
simon2
simon8
simon53
I want to take only last modified files (the newest) and copy them to different location (/home/user/fixed) without that numbers in file name.
I know how to f... |
The following is hopefully self explanatory
find -maxdepth 1 -mtime -2 -type f -exec bash -c 'name=${1##*/}; cp "$name" /some/other/dir/${name%%[0-9]*}' _ {} \;
| find file, copy but with different name |
1,425,620,266,000 |
I want to using crontab to synchronize two directory between my linux partion and windows partion like this:
24 9 * * * cp -r /home/fan/Data /media/T/Data
But it would create a directory named Data in the origin Data directory, instead of copy the missing file from the source directory. I can't find a proper option... |
To address the error-message portion of the question, you might choose to run a script from cron instead of the system command.
24 9 * * * /usr/local/sbin/sync_data.sh
Create the file as /usr/local/sbin/sync_data.sh, giving root ownership and execute permission: chown root:root /usr/local/sbin/sync_data.sh && chmod 0... | how to synchronize two directory? |
1,425,620,266,000 |
I need to copy huge files in my Linux machine.
Example:
cp source.txt target.txt
I want to create bar progress that will show that copy still in progress on each copy file
Examples"
cp file file1
copy file > file1 .........
cp moon mars
copy moon > mars .......
|
In short, you won't find cp native functionality for progress bar output. Why? Many reasons. However, you have some options:
Use a different tool. rsync, as mentioned by @user1404316 has --progress:
rsync -P largeFile copyLocation
If you don't need the extra semantics that cp and rsync take care of, create a new... | how to create progress bar during copy files |
1,425,620,266,000 |
I have more than 90 subdirectories and inside each one, there will be a number of .txt files.
What I need to do is to copy all those txt files out to one single directory.
How can I do that?
|
use command :
find . -name "*.txt" -exec cp {} /path/to/destination \;
| How use minimum number of commands to copy all .txt files from all subdirectories to one directory? |
1,425,620,266,000 |
EDIT: Total rewrite of question for clarity.
I have a directory tree (new) with a bunch of files of with an extension of .new. I have an identical tree (old) where many of the files have names identical to those in the new tree except that the extension is .old. I would like to copy all of the .new files from the new... |
This was the final answer that got hashed out in the comments for terdons answer.
cd new
for i in */*/*.new; do cp "$i" "path/to/old/${i}" && rm "path/to/old/${i//new/old}"; done
| Need to copy files to existing directory and remove files already there with the same name but different extension |
1,425,620,266,000 |
I'm trying to copy files from an USB stick to another drive. At least the file names appear to be corrupt, ls shows them as:
'ZHECMIv'$'\027''.PDF'
'ZHEKMI>2.P─F'
ZHENIL~1.PDF
'эeloѤyfɯrɥvdr.2uOroä䁲igez_o_聴eŢe'$'\340\240\256''Ű聤f'
'ၙanPѥòѳen-ၐoint-M䁯rѴ&`df'
Copying fails with errors like these:
cp: error reading ... |
cp: error reading <filename>: Input/output error indicates there is corruption in locations other than filenames too.
The fdisk output is normal for a GPT-partitioned external disk. The size does not match the 32 GB you said, but 1.82 TiB is consistent with the disk model Elements SE 25FD reported by fdisk: are you re... | Copying files from USB drive fails - I/O error or Invalid Argument |
1,425,620,266,000 |
When I don't need to adjust destination filenames I can do something like this:
$ find -type f -name '*.pat' -print0 | xargs -O cp -t /path/to/dest
It is safe because the filenames may even contain newline characters.
An alternative:
$ find -type f -name '*.pat' -print0 | cpio -p -0 -d /path/to/dest
Now I have the ... |
With pax as found on Debian, Suse, OpenBSD, NetBSD at least:
find . -type f -name '*.pat' -print0 | pax -0rws'/?/_/gp' /path/to/dest/
pax is a standard utility (contrary to tar or cpio), but its -0 option is not, though can be found in a few implementations.
If there's both a ?.pat and _.pat files, they will end up r... | How to copy a list of files and adjust destination filenames on the fly? |
1,425,620,266,000 |
Suppose I have host A from which I ssh to host B, where I sudo -U some_role and from under it ssh to host C. My goal is an interactive shell on C.
Assume that from C I cannot ssh back to A.
What is the best way to copy a file from A to C using the connection built above? What preparations / changes should I introduce ... |
I take it host B is e.g. a gateway in an intranet and can connect to host A and C, e.g. like this:
-----------------------------------------------------------
| ... | Copy files via a complex ssh connection? |
1,425,620,266,000 |
General question
Assuming two directories with identical content are stored on different devices. Is there a way to calculate the size of the directories and always get the exact same number for both?
In other words, is there such a thing as a "real size" of a directory irrespective of where it is stored?
Practical ex... |
Note that du, even GNU's one with its --apparent-size option will include the apparent size (as reported by lstat()) of all types of files, including regular files, devices, symlinks, fifos, directories. GNU du like many other implementations will try to not count the same file several times (like when there are sever... | Get size of directory (including all its content) irrespective of disk usage |
1,425,620,266,000 |
The Situation:
I want to copy a directory recursively to an external hard drive. The directory contains a lot of files (at least 100,000).
The Problem:
External hard drives tend to get quite hot when heavily used like in my task (usage over several hours). That is bad for the life expectancy. So since time is not an ... |
Not particularly elegant, but you could run your copy command and then run a loop that pauses it for, say 3 minutes every 20 minutes:
Launch your copy command in the background
cp -r /path/to/dir /path/to/external/drive &
Run this loop which will stop/restart it:
while ps -p $! >/dev/null; do
kill -SIGCONT $!; ... | Copy a lot of files recursively leads to a heat problem on my external hard drive |
1,425,620,266,000 |
I've been searching and I cannot find an answer on any of these sites that will work. I want to copy all of the contents of this folder to my server. Which Linux command should I use to do that? I don't want to manually run through all of the objects in that folder, one at a time per se. I've seen everything from scp ... |
you can use something like
cd directory-where-you-want-to-put-the-files
wget -r ftp://ftp.eso.org/pub/qfits/
| Copy contents of remote server folder to current server? |
1,425,620,266,000 |
I need to copy a particular named file from multiple directories and need to add number in the file prefix sequentially. For example I have the following directories, gene1, gene2, gene3 ..... gene100 and each directory having a file namely protein.fasta. I need to copy all the protein.fasta file from each directory a... |
You should be able to loop over directories and remove the gene prefix from the current directory name to use as the prefix for the target file name:
for d in gene*; do
echo cp "$d/protein.fasta" "output/${d#gene}_protein.fasta"
done
Remove the echo once you are satisfied that it is doing the right thing.
| copy a file from multiple directories and add numbers in the prefix of each file? |
1,425,620,266,000 |
The sequence of commands
mkdir -p BASE/a/b/c && cp -a --target-directory=BASE/a/b/c /a/b/c/d
creates a subdirectory a/b/c under BASE, and then copies the directory tree at /a/b/c/d to BASE/a/b/c.
One problem with it is that it entails a lot of repetition, which invites errors.
I can roll a shell function that encapsu... |
With pax (a mandatory POSIX utility, though not installed by default in some GNU/Linux distributions yet):
pax -s':^:BASE/:' -pe -rw /a/b/c/d .
(note that neither --target-directory nor -a are standard cp options. Those are GNU extensions).
Note that with -pe (similar to GNU's -a), pax will try and copy the metadata... | "standard" single-command alternative for `mkdir -p BASE/a/b/c && cp -a -t BASE/a/b/c /a/b/c/d`? |
1,425,620,266,000 |
I'm rsync'ing a huge folder from an external to an internal 3,5" HDD, both 5.400 rpm. When using dstat to have a look at the current throughput, I regularly see patterns like this:
--total-cpu-usage-- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai stl| read writ| recv send| in out | int csw
2... |
There are 2 sets of tools to get some block-level device statistics. The first is iolatency from Brendan Gregg's perf tools. It produces a simple histogram of disk operation latency such as:
>=(ms) .. <(ms) : I/O |Distribution |
0 -> 1 : 1913 |############################... | Causes for inefficient I/O? |
1,425,620,266,000 |
I was trying to copy one folder from one location to another. The folder is about 6.4 Gb.
So I did
cp -r source_folder level1/val
after that, I went into the level1 folder and checked:
level1$ ls
val
But If I try to cd into val, an error is raised:
level1$ cd val
-bash: cd: val: No such file or directory
And it d... |
Evidently, the val that resulted from the copy the first time round is a broken symbolic link.
ls lists val because it exists: there is a directory entry called val.
cd val complains “No such file or directory” because val is a broken symbolic link. cd needs to access the target of the link, but the target doesn't ex... | ls shows a directory but it is inaccessible |
1,425,620,266,000 |
I have an approximately 1TB directory with subdirectories dir1. I have made an rsync backup copy dir1.back.
How can I efficiently restore dir1 to the state of dir1.back - that is, replace files in dir1 with those of dir1.back if they've change and deleting any new files in dir1? Given its large size, cp/rsync of the e... |
Reverse the RSYNC. So swap around dir1 and dir1.back then add the delete flag to ensure it removes files as appropriate to make sure it properly syncs and not just ignore files that weren't present in dir1 originally.
rsync -avz --delete dir1.bak/ dir1/
| Restore directory to previous state |
1,425,620,266,000 |
I have several audio devices (car radio, portable radio, MP3 player) that take SD cards and USB sticks with a FAT file system on it. Because these devices have limited intelligence they do not sort filenames on the FAT FS by name but merely play them in the order in which they have been copied to the SD card.
In MS DO... |
I remember asking this a long time ago (you are welcome to search for it). My guess at this long future time is: mount the device with option sync (removes the buffering), sort the list to ensure that they are copied in order.
| File order on FAT/FAT32/VFAT file systems |
1,425,620,266,000 |
I have a long list of data files that I need to copy over to my server, they have the names
data_1.dat
data_2.dat
data_3.dat
...
data_100.dat
Starting from data_1.dat, I would like to get all the files where the number is increased by 3, i.e. data_4.dat, data_7.dat, data_10.dat, ...
Is there a way to specify this? Ri... |
On Linux:
printf -- '-get data_%d.txt\n' $(seq 1 3 100) | sftp -b - [email protected]
On BSD (with no seq(1) in sight):
printf -- '-get data_%d.txt\n' $(jot 100 1 100 3) | sftp -b - [email protected]
| sftp: command to select desired files to copy |
1,425,620,266,000 |
I want to find images, from a directory, that were added in last 1 year and copy them to a new directory preserving original folder structure.
I am using find but it is not copying anything. is there any way it can be done using 1 line command?
find image/* -mtime +356 -exec cp {} modified-last-year/ \;
I am in th... |
You can use find and cpio in passthrough mode:
find image/ -mtime -365 | cpio -pd /target_dir
EDIT: removed unnecessary * from the find path.
| find modified files recursively and copy with directory preserving directory structure |
1,425,620,266,000 |
I have a working directory: /home/myusername/projectdir
The working directory contains files and sub-directories. The depth of sub-directories is not known.
I want to put all the *.log files into the same output directory, with the base-names prefixed with the sub-directory path (substitutin / with #).
Example:
/hom... |
By using \0-delimited strings, this can handle spaces and \n in file names.
cd "${PROJECT_DIR%/*}"
outdir="output"; mkdir -p "$outdir"
find "$PROJECT_DIR" -type f -name '*.log' -printf "%p\0${outdir}/%P\0" |
awk 'BEGIN{FS="/";RS=ORS="\0"}
NR%2||NF==2 {print; next}
{gsub("/","#"); sub("#","/#"); print}... | Copy filenames, and add path prefix, in a directory, recursively |
1,425,620,266,000 |
If I decide I want to copy a folder that is sufficiently large using cp, then half way through the copy I decide to abort or pause the process, will this ever cause corruption? Would it be better to let the copy finish and then delete the files?
|
If you pause the process and resume it later, nothing bad will happen. As long as nothing else writes to the input or output file in the middle, the output will be a faithful copy of the original.
If you kill the copy process, then you'll end up with a partial copy in the output file. There's no point in waiting: if y... | can cancelling a copy cause corruption? |
1,425,620,266,000 |
I accidentally moved an entire directory of ~100GB to trash. I was trying to place it in bookmarks but dragged it into trash. It's there in the trash. But
when i try to restore I run out of space on the disk
Prior to deletion I had less than 50GB free on disk, if I need to restore the normal way I need about 68GB m... |
if you are moving to the same partition then
mv /source/* /dest/
should work without creating a copy or consuming more space
Alternatively, just do the same exercise with /dest/ on an external drive or partition then copy them back once you have cleared space in your original location.
| Accidentally trashed large file |
1,508,107,905,000 |
I have a hard time using Linux' built-in tools to rip an audio cd (sound juicer, rhythmbox). The reason likely being my drive, which vibrates a lot and cannot read the disk continuously. Playing the disk in any audio player results in short pauses and stutter-y playback. Ripping the CD results in noticeable artefacts.... |
To rip an audio CD you should really use a tool such as cdparanoia.
This will handle jitter and error correction, will retry as necessary, and try to create a "perfect" datastream.
Typically you would use this to create the wav files, which can then be converted to FLAC format as necessary.
There are other tools, incl... | How can I copy a .wav file from an audio cd and verify it? |
1,508,107,905,000 |
I've little inconvenience while copying image files from another Android Project to my current one.
Suppose I've files called nice_little_icon.png in each of the directories drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi, which are under res directory of Project1.
Now how do I copy the... |
You can use the pax command (a standardized replacement for tar and cpio). This command is present on all POSIX-compliant systems, but beware that some Linux distributions omit it from their default installation. pax copies each path under the destination directory.
pax -rw -pe drawable-*/nice_little_icon.png ../../Pr... | How to copy multiple files with a same name from children directory to another directory without losing the parent directory? |
1,508,107,905,000 |
I want to find all files in dir1 having corresponding same file names in dir2, and replace them with the files from dir2.
For example:
dir1: first.txt second.txt
dir2: third.txt first.txt
So I want to remove from dir1 the old first.txt file and replace it with first.txt from dir2.
How to achieve this using Bash termi... |
Actually, there's a single command that does exactly what you're asking.
rsync -av --existing dir2/ dir1/
This will recursively copy the files from dir2 into dir1 only if the file already exists in dir1.
The -av options are the options you'll usually use for copying files using rsync.
The --existing option tells rsyn... | Find and replace all same files between 2 directories |
1,508,107,905,000 |
I am doing backups of a directory with a simple:
cp -R /production/directory /backups/location
Sometimes I need to restore a backup, but doing:
cp -R /backups/location/* /production/directory
or
cp -RT /backups/location /production/directory
has the unwanted (in my case) effect, of keeping files present in /product... |
You can use rsync to achieve what you want.
rsync -r --delete-during /backup/location/ /production/directory
For more on see man rsync
| How to copy a folder by another, but NOT by merging/adding |
1,508,107,905,000 |
Assume an rsync --recursive --ignore-existing foo bar copy command was being run for a large directory tree named foo, but that that command got prematurely interrupted. For example, because of a sudden power failure on the target machine.
Running the above command again can save a lot of time for any files/dirs alrea... |
You can use the -c option to make rsync skip files by calculating and comparing their checksum instead of date and size:
-c, --checksum
skip based on checksum, not mod-time & size
That should copy all files again that doesn’t have the right contents.
You also have to remove the--ignore-existing flag, otherwise files... | rsync --recursive --ignore-existing: what (if any) additional flags are needed to resume/repair/overwrite any partially copied files? |
1,508,107,905,000 |
RHEL 7.9 x86-64
high end dell servers with Xeon cpu's, 512gb ram, intel nic card
I am the only user on the server(s), and there is no other work load on them
cisco 1gbps wired LAN
data.tar is ~ 50 gb
/bkup is NFS mounted as vers=4.1 and sync
a scp data.tar backupserver:/bkup/ runs at 112 MB/sec consistently; I've see... |
The primary cause is probably the fact that the NFS share is mounted with the sync option. This theoretically improves data safety in scenarios where the server might suddenly disappear or the client might unexpectedly disconnect, but it also hurts performance.
Using the sync mount option is equivalent to the applicat... | why is NFS copy speed half that of SSH scp? |
1,508,107,905,000 |
When copying large files (1-2 GB per file) between file systems, file fragmentation can happen if the destination file system is nearly full.
Our C++ application code uses fallocate() to pre-allocate space when creating and writing data files but I'm wondering how the linux copy command /bin/cp handles that.
Does cp j... |
The version of cp provided by GNU coreutils does use fallocate, but only to punch holes in files, not to pre-allocate space for copy targets.
There are a couple of mentions of adding support for fallocate, so it appears there were at least vague plans for something like this at some point.
| Does cp (copy) pre-allocate space with fallocate()? |
1,508,107,905,000 |
I have to move my user home to another device, it occupies several gigabytes, I would like to avoid losing something, at the moment I think I will use rsync rsync --progress -avh --remove-source-files $SRC/ $DST/, Is there anything better?
|
If you want to be on the really safe side (depending on your level of paranoia):
Burn in the new device
Perform surface test of new device
Copy files to new device (i.e. not using the option --remove-source-files)
Mount the new device in its intended place and make sure everything works correctly
Only when everything... | Linux, safest way to move a file system |
1,508,107,905,000 |
I know how to use the cp command to recursively (-r) copy nested folders while preserving (-p) file meta-data such as modification times:
cp -pr "/path/from" "path/to"
Is there a way to omit one particular folder nested at the first level deep, by name?
For example, a folder named Photos is being copied. I want to sk... |
Yes, you can fairly easily make a copy of a file structure while avoiding copying one or several of the subdirectories, but you would not do it with cp.
With rsync, you can exclude files and/or directories using an exclusion pattern. In your case, it looks like you'd want to use
rsync -av --exclude='Photos/Dogs/' /pa... | Omitting one particular nested folder while copying a folder |
1,508,107,905,000 |
I have a folder that contains 1208 folder. In each of these folders, I have 6 different files which follows a special naming criteria.
What I need to do is to get only one of these files from all the 1208 folders if it contains the following in its name: _fa_a
The hard way is to go into each of the folders and copy th... |
find your_folder -type f -name "*_fa_a*" | while read filename; do echo mv "${filename}" destination_folder; done
this find command finds the file and move to the destination_folder.
i added echo command for you to verify the results before move it. once you are happy with echo command output, remove the mv command.
| Extract a single file from a list of directories in Linux |
1,508,107,905,000 |
I've copied a large directory to another location (through a network). I needed to preserve all the timestamps (especially ctime and mtime).
However somewhere in the process I screwed things up. (I probably made a typo in the flags.) And all the files have new timestamps now.
I still got the directory with the correc... |
Yes, rsync is your best bet. Something like this should work:
rsync -vr --size-only --times <source> <dest>
--size-only tells rsync not to copy the files again, --times tells it to update timestamps.
| Fit timestamps of multiple files/folders to existing ones |
1,508,107,905,000 |
I'm trying to replace my cp function by an rsync function
My cp function is the following
find /home/odroid/USBHD/Movies/ -iname "*.mkv" -mtime '-1' -exec cp -n {} /home/odroid/NASVD/Movies \;
do you guys have any idea how to do this(note the mtime can also be replaced by --ignore-existing
|
find /home/odroid/USBHD/Movies/ -iname "*.mkv" -mtime '-1' -print0 | xargs -0 -I{} rsync -a --ignore-existing {} /home/odroid/NASVD/Movies/
| Replace cp function by rsync [duplicate] |
1,508,107,905,000 |
I wanted to generate some waste file of 50 GB. so i wrote this
eightnoteight@mr:~/ while true; do
> cat txt >> tmp
> cat tmp >> txt
> done
and when i ran top,watch to observe. I noticed that in top the memory consumption of cat is 0.0
If cat is not consuming my memory who is doing the work? (Is it direct kernel calls... |
I believe you are getting misled by the rounding in the %MEM column. If you note the VIRT and RSS column, they report the amount of virtual memory and resident memory used. In both cases you can see that they are non-zero.
Virtual memory is the amount of virtual memory the process has, including shared libraries and... | top not showing the memory usage of cat |
1,508,107,905,000 |
I have a bunch of files with a pattern like this:
{UID}-YYMMDD-HHMMSSMM-NEW.xml
Or a real example:
56959-140918-12465122-NEW.XML
I want to copy these files to another directory in date and time order contained within the filename. I also only want to apply this to filenames that match the above pattern.
Are there an... |
If all the matching files are in the current directory (and not in any subdirectory or if the subdirectory names do not contain -), you can use for step 1 to 3:
find -regex '.*/[0-9]+-[0-9]+-[0-9]+-NEW\.XML' | sort --field-separator=- --key=2 > filelist
and for step 4:
while IFS= read -r line; do
cp -v $line /PATH/T... | Copy files by date/time order contained within filename? |
1,508,107,905,000 |
I'm not entirely sure where to ask this question, so if this is not the right place just let me know and I'll move it.
Today I was trying to using recursive scp to copy a directory between two Linux servers and made a typo when referring to the destination server. Instead of transferring the directory to the other mac... |
For copying files on the same machine you wouldn't need scp at all. Anyway, if you specify a directory or file as destination instead of a hostname and a path it will copy it for you locally, which seems to be what happened. If you supply the command line you used we can point you what happened exactly.
EDIT:
With the... | Interesting Secure Copy Behavior |
1,508,107,905,000 |
I have two directories that look something like the following but with many more files.
folder1/pic1.png
folder1/test/readme.txt
folder2/guest.html
folder2/backup/notes.txt
I want to "merge" these two so all the contents of folder2 end up in folder1 and folder2 gets removed. They are on the same filesystem and disk ... |
The "rsync" command is useful for this. I do something like this:
rsync -PHACcviuma --copy-unsafe-links --exclude="*~" folder2/ folder1/ && rm -fr folder2
All the flags are documented in the rsync man page; basically rsync won't replace newer files with older ones, and won't bother to copy any files that are dup... | How should I merge two folders on the same filesystem? |
1,508,107,905,000 |
Is there a Linux command to copy a_b_c_d.jpg into a.jpg, b.jpg, c.jpg, and d.jpg so that each file is a copy of the original?
It should extract the name from the original name, separated by _ and ending with the first ..
|
Mayhap something along this line:
for FN in *.jpg; do IFS="_."; AR=($FN); for i in "${AR[@]}"; do [ ! "$i" = "jpg" ] && echo cp "$FN" "$i".jpg; done; done
cp a_b_c_d.jpg a.jpg
cp a_b_c_d.jpg b.jpg
cp a_b_c_d.jpg c.jpg
cp a_b_c_d.jpg d.jpg
You may want to add some more error checking, IFS safe guarding, remove the ec... | Is there a linux command to copy a file to multiple other files based on the filename? [closed] |
1,508,107,905,000 |
Referencing this post to find and delete duplicate files based on checksum, I would like to modify the approach to perform a copy operation followed by a file integrity check on the destination file.
SOURCE = /path/to/Source
DEST = /path/to/Destination
# filecksums containing the md5 of the copied files
declare -A f... |
Implementing something like this might be hard as a first try if you care about the files and don't want to mess up. So here are some alternatives to writing a full script in bash. These are more or less complex command lines (oneliners) that might help in your situation.
There is one uncertainty in your question: do ... | Help with script/rsync command to move file with md5 sum comparison before deleting the source file/ [closed] |
1,508,107,905,000 |
Is there any difference between:
cp -R /a/* /b
and
cp -R /a/. /b
The original idea was to copy anything from folder /a into folder /b.
|
The only difference is that the first command,
cp -R /a/. /b
would copy hidden files and directories from /a to /b, while the second command,
cp -R /a/* /b
would not do so.
The reason for the second command not copying hidden files is that the * expands to all the non-hidden names in /a (unless the shell option dotg... | Difference in cp -R argument? |
1,508,107,905,000 |
I want to copy and rename multiple files from one directory to another.
Particularly, I want something like this:
/tmp/tmp.XXX/aaa.original.txt
/tmp/tmp.XXX/bb5.original.txt
/tmp/tmp.XXX/x2x.original.txt
copied into
/root/hello/dump-aaa.txt
/root/hello/dump-bb5.txt
/root/hello/dump-x2x.txt
I've tried some things li... |
bash solution:
for f in /tmp/tmp.XXX/*.original.txt; do
bn="${f##*/}" # extracting file basename
cp "$f" "/root/hello/dump-${bn%%.*}.txt"
done
| How to copy and add prefix to file names in one step from another directory? |
1,508,107,905,000 |
We want to migrate contents of an old server to a new one but rather than coping everything, we want to exclude any directory that has a .swf file in it. We are aware of the --exclude flag but it will only exclude the file(s), not the parent directory (and it's contents), if there is a .swf file
If this cannot be done... |
You could generate a list of directories which contain *.swf files, and then convert that list into an exclude file for rsync.
e.g.
find /path/to/topdir/ -iname '*.swf' -printf "%h\n" |
sort -u |
sed -e 's/^/- /; s:$:/**:' > rsync-exclude.txt
output will look something like this, which can be used with rsync'... | rsync - copy contents of the directory only if a certain file doesn't exist |
1,508,107,905,000 |
I want to copy a Windows 7 partition that came installed on my laptop to my desktop computer.
I've tried:
# bzip2 -c /dev/sda5 | nc 192.168.1.1 2222 # on laptop
# nc -l 2222 | bzip2 -d > /dev/sda1 # on desktop
But gparted tells me the partition is corrupted with a lot of error messages.
I also tried:
# dd if=/dev/sda... |
I finally copied using a tar pipe.
# cd /mnt/sda1/ && tar cf - * | nc 192.168.1.1 2222 # on laptop
# cd /mnt/sda5/ && nc -l 2222 | tar x # on desktop
Copying was way faster and seemed to work.
I wasn't able to boot in Windows 7 thought. I only saw a black screen when booting in it and the recovery partition freezes a... | How to copy a partition over network |
1,508,107,905,000 |
I'm looking to copy files from subdirectories that match this pattern
vendor/plugin/*/tasks/*.rake
into a folder
lib/tasks
|
Sounds pretty easy:
cp vendor/plugin/*/tasks/*.rake lib/tasks
Or if the first * should match a whole subtree, use something like:
find vendor/plugin -path "*/tasks/*.rake" -exec cp "{}" lib/tasks +
| How to copy files nested in directories that match a pattern? |
1,508,107,905,000 |
How can I copy files between two hosts the first primary host is running Linux and the secondary host is running Windows.
I am looking for a correct command line to use it in terminal/Linux?
I tried
scp user1@remote1:/home/file user2@remote:/home/file
But it didn't work.
Any suggestions ?
|
On Linux, install and run the SSH daemon sshd (the package is openssh-server in most distributions). Then from Windows download and use WinSCP to connect to the Linux machine and transfer your files in both directions.
Or - to do this the other way around - install the SSH server freeSSHd on Windows, then from Linux... | Copy files between two hosts |
1,425,693,332,000 |
I am installing an rpm package and it appears to be skipping certain files without giving me any notice as to what the issue is.
When I execute
rpm -ivh package_name.rpm
the rpm provides me with no indication that the installation failed.
After executing this, I verify the installation:
rpm -V package_name
And I se... |
After some work a solution was found. Inside the rpm, a few dos2unix calls were made. A coworker of mine was able to determine that the verson of dos2unix that was installed had some issues.
After upgrading to the latest version, the u2dtmp* files disappeared.
| RPM skipping files on install |
1,425,693,332,000 |
I have a file test.txt in directory A/B. I want to copy test.txt to A/C and rename it newtest.txt.
I know I can use the cp and mv commands to do this, but the issue is that there already is a test.txt in A/C, and there's already a newtest.txt in A/B and I don't want to overwrite either of those files.
I know I technic... |
Just do
$ cd A/B
$ cp test.txt ../C/newtest.txt
Use
$ cp -i test.txt ../C/newtest.txt
to check whether ../C/newtest.txt (i.e., A/C/newtest.txt) already exists
and ask for confirmation.
(I almost never deliberately overwrite files,
so I alias cp to cp -i to get this protection every time I do a cp.
But it’s also wi... | How can I copy a file and paste it as a different name? |
1,425,693,332,000 |
I need to make a copy of my home directory and place that copy in the same home directory. The exit code of the command must be 0. Currently, my home directory does not contain any other directories.
Is there a better way than the following? (pwd is the home directory)
mkdir /tmp/temp && cp * /tmp/temp && mv /tmp/temp... |
Call rsync and exclude the directory where you're putting the copy.
cd
mkdir copy
rsync -a --exclude=copy . copy
Copying * excludes dot files (files whose name begins with a .), which are common and important in a home directory.
| How to copy the home directory in the home directory? |
1,425,693,332,000 |
I was copying a very big file and I accidentally stopped it. Can I resume copying data without need to delete copy and copy data again?
Command I used:
pv original.data > copy.data
|
Continue with dd:
dd if=original.data of=copy.data ibs=512 obs=512 seek=NNN skip=NNN status=progress
You have to get byte count in the copy.data. Then replace NNNs with byte count divided by 512 (value set to ibs and obs).
| Continue copying file |
1,425,693,332,000 |
I would like to copy files in an remote server from one directory (in the remote server) to another (also in the same remote server). I tried this:
scp -r [email protected]:/folder_a/*myfiles* ../folder_b
This did no give any error message, but it did not work. I also tried this:
scp -r [email protected]:/folder_a/*m... |
You should use ssh and do:
ssh [email protected] "cp /folder_a/*myfiles* /folder_b"
| How to copy files within a remote server? |
1,425,693,332,000 |
So I was trying to download a file from a remote host connected with SSH to a local folder on my Mac using rsync. The command I used is:
$ rsync --progress -avz -e "ssh [email protected] -i ~/.ssh/keyFile" [email protected]:/path/to/files/ ~/Downloads/
And here is what is prompted:
[email protected]'s password: [I ty... |
I'm pretty sure that you don't want to include the user@host part of the ssh command within the -e flag. Try
rsync --progress -avz -e "ssh -i ~/.ssh/keyFile" [email protected]:/path/to/files/ ~/Downloads/
| rsync thinks the source host is a command |
1,425,693,332,000 |
I am running a PHP script on my Apache server and from the script I need to copy some files (to run a Bash script that copies files). I can copy to a directory /tmp with no problems, but when I want to copy to /tmp/foo then I get this error:
cp: cannot create regular file '/tmp/foo/file.txt': Permission denied
even t... |
/tmp Directory has all the permissions (read/write) for all users. but if you made /tmp/foo by your own account, it has its permissions just for you! if you want to make it writable for other users (or programs) change its permission with this command:
chmod 777 /tmp/foo
If you have any other files inside this directo... | cp: cannot create regular file: Permission denied |
1,425,693,332,000 |
I have one file, let's call it image.png. I also have a folder of files like so:
picture.png
file.png
screenshot.png
art.png
painting.png
And so on.
What I want to do is replace each file inside the folder with image.png, but I want to keep the name of the original (so picture.png is still called picture.png, but whe... |
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 ge... | How do I replace all files in a folder with one file? |
1,425,693,332,000 |
I got a git directory with plenty of python files(and some special file like .git).
I'd like to copy only these python files to another directory with the directory structure unchanged.
How to do that?
|
You will receive in destination_dir files with full path from /
find /path/git_directory -type f -iname "*.py" \
-exec cp --parents -t /path/destination_dir {} +
Other solution is rsync
rsync -Rr --prune-empty-dirs \
--include="*.py" \
--include="**/" \
--exclude="*" \
... | How to copy a directory with only specified type of files? |
1,425,693,332,000 |
I installed SentOS 8 with Xfce and there is not many gtk themes in it. Can I simply copy them from Mint 20 Xfce? Is it legal to copy the contents of the two folders /usr/share/icons/ and /usr/share/themes/ between different linux distros?
|
There shouldn’t be any problem in the majority of cases; exceptions might include themes with branding (but I haven’t checked).
You’ll find the license terms for the various files involved on most if not all distributions, included in the distribution. For Debian-based distributions, find the packages involved using d... | Is it legal to copy themes between different linux distros? |
1,425,693,332,000 |
I have a file in a very long path, for example:
/opt/very/long/path/file1
I want to copy the file in this directory:
cp /opt/very/long/path/file1 /opt/very/long/path/file2
I don't want repeat this long path. I can go to the destination folder and copy:
cd /opt/very/long/path/
cp file1 file2
But I don't want change ... |
Brace expansion is nice for this:
cp /opt/other/very/very/long/path/{fileA,fileB}
... will expand to:
cp /opt/other/very/very/long/path/fileA /opt/other/very/very/long/path/fileB
when it actually executes.
The command will show up in your history as you typed it, which preserves the paths:
$ history
# ...
508 c... | Copy a file in a destination folder |
1,425,693,332,000 |
I'm using the following command to get the most recent file in a directory
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/"
This returns only the file name not the entire path.
I then want to copy the file I found into another folder, so I append the ... |
Your command appears to have two issues, the first of which may not matter much in your case, but is nevertheless worth pointing out: (i) it is not generic in the sense that it will not be able to process arbitrary filenames, in particular filenames that contain newlines (i.e. \n), and (ii) as already noted by Kusalan... | Find and copy with exec not working |
1,425,693,332,000 |
I can use rm to delete the old folder, then use cp to copy the new folder. But how to do it in one go?
|
Using rsync:
rsync -av --delete source/ target
This would delete all the contents of the directory target that does not match the contents of the directory source, and would additionally copy the contents of source there.
The trailing / at the end of source/ is significant as without it, you would get a directory at ... | How to copy a folder by overwriting an existing folder and delete all the old content in Linux? |
1,425,693,332,000 |
I am trying to copy files from one path to another path. I have a text file which has all names of files in the following pattern:
file-1.txt
file-2.pdf
file-3.ppt
....
I created a .sh file with the following code:
#!/bin/bash
file=`cat filenames.txt`;
fromPath='/root/Backup/upload/';
toPath='/root/Desktop/custom/up... |
file=/path/to/filenames.txt
fromPath=/root/Backup/upload/
toPath=/root/Desktop/custom/upload/
cd "$fromPath" && xargs mv -t "$toPath" < "$file"
| copy files from one path to another path in linux |
1,425,693,332,000 |
I have 2 external drives and I want to use rsync to copy the files that have been updated (modified timestamp) in the source directory to a target directory.
The files have the same filename but the timestamp is different i.e. some files have been recently updated but the filename has remained the same.
However,
rsync... |
Well, Reading The Fine Manual I find this:
--ignore-existing skip updating files that exist on receiver
So by definition, the options you're using are explicitly asking NOT to update already existing files.
I think you simply want to use "-a" (archive) option:
rsync -av --progress /Volumes/vol1/Data/ /V... | Rsync unexprected behavior |
1,425,693,332,000 |
I am trying to switch from one hard drive to another. So I decided to boot in Linux, hook both hard drives up, and copy all files from one hard drive to the other.
However, when I try to copy protected Windows-10 files, such as C:\Windows\explorer.exe or C:\Windows\notepad.exe, I get the following error:
cp: cannot a... |
If you just copy files from one NTFS partition to another there's a high chance your Windows won't boot at all. You'll need to use ntfsclone for that.
Speaking of your specific error: you're most likely missing an NTFS-3G compression plugin. It's not clear what your distro is but in Fedora the package is called ntfs... | How to copy Windows 10 system files in Linux? |
1,425,693,332,000 |
I am writing a shell script to do some complex task which is repetitive in nature.
To simplify the problem statement, the last step in my complex task is to copy a file from a particular path (which is found based on some complex step) to a pre-defined destination path. And the file that gets copied will have the perm... |
Just include the chmod in your xargs call:
...| xargs sh -c 'for file; do
cp -- "$file" /my/destination/path/ &&
chmod 700 /my/destination/path/"$file";
done' sh
See https://unix.stackexchange.com/a/156010/22222 for more on the specific format used... | How to copy a file and change destination file permission in one step? |
1,425,693,332,000 |
I use the following command to find a file and copy it somewhere else,
find /search/ -name file.txt -exec cp -Rp {} /destination \;
How can I copy all files and subdirectories in the parent directory of file.txt?
Example,
/search/test/sub
/search/test/sub2
/search/test/file.txt
/search/test/file.doc
They should be c... |
With -execdir (not a standard predicate, but often implemented), the given utility would execute in the directory where the file was found.
This means that you could do
find /search -name file.txt -execdir cp -Rp . /destination \;
Without -execdir:
find /search -name file.txt -exec sh -c 'cp -Rp "${1%/*}/." /destinat... | How to find a file and copy its directory? |
1,425,693,332,000 |
I am using Cygwin as Linux shell, I have following contents in my current working directory:
Files :
Abc.dat
123.dat
456.dat
Directories:
W_Abc_w
W_123_w
W_456_w
Now I want to copy files as below:
Abc.dat -> W_Abc_w
123.dat -> W_123_w
456.dat -> W_456_w
How to achieve this in a single line linux command? ... |
In one command (line):
cp Abc.dat W_Abc_w/; cp 123.dat W_123_w/; cp 456.dat W_456_w/
The trailing slashes are not required, but a habit to indicate that the intention is to put the file into a destination directory not as a new file.
As a generic loop with a pattern:
for f in ???.dat
do
[ -d W_"${f%.dat}"_w ] && cp... | Copy files such that individual files gets copied to the folder having file name as a string within complete folder name |
1,425,693,332,000 |
I have a symlink to a file on my Ubuntu system, and I need to copy the original file to a different directory and have a new name there. I am able to copy it to a different directory using
readlink -ne my_symlink | xargs -0 cp -t /tmp/
But I am not able to give a new name in the destination directory.
Basically, I a... |
cp will dereference symlinks with -L option.
This should work:
cp -L my_symlink /tmp/newnametofile
Regarding your xargs, -t, --target-directory option of cp only takes DIRECTORY as input. You could make it work using xargs -I{} cp {} /tmp/newnametofile (but I'd use cp -L anyways...
| copying a symlink to a target file using cp -t |
1,425,693,332,000 |
I am using Ubuntu 18.04 LTS and
I wish to copy files from one folder to another and along with it also wish to store the filepath of each file that's being copied from folder1 to folder2 in a third file with space as a separator.
NOTE: Since cp command does not return any output so storing it in file won't work.
An... |
A combination of find and cpio can be used to recursively copy all files and subfolders from folder1 to folder2. With tee in between you can write all file names (relative to folder1) into outputfile.
cd folder1 && find . -depth | tee outputfile | cpio -pdm folder2
The command cd folder1 is necessary because cpio wan... | Get filepath of every file that's copied from one folder to another |
1,425,693,332,000 |
We need to do a once-only archive copying of users' home folders to an archive server (pending final deletion) when they leave, in case they later discover that they may still require some of their files (although we do of course very strongly encourage them to take their own backup of everything they might still need... |
If you like tar except for the temp file, this is easy: don't use a temp file. Use a pipe.
cd /home ; tar cf - user | gzip | ssh archivehost -l archiveuser 'cat > user.archived.tar.gz'
Substitute xz or whatever you prefer for gzip. Or move it over to the other side of the connection, if saving CPU cycles on the main ... | Archiving user home folder to remote server, without following symlinks? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.