Make a copy of a file and give it a different name mac terminal - macos

Mac.
I'm in a directory dogs/scripts/cats.
Within this directory there is a file bla.txt.
I would like to make a copy of bla.txt called bla2.txt and keep it in the same directory.
How do I do that?
cp bla.txt dogs/scripts/cats
'bla.txt' and `dogs/scripts/cats/bla.txt' are the same file

cp can get a name of a target file:
cp bla.txt ./bla2.txt
Or even simpler, as Mark noted:
cp bla.txt bla2.txt

Related

Copy files to their corresponding destination folders

I have many files in a folder with different extensions (.txt, .ascii, .mat). I want to move them to the destination folder which would be same as file names.
For example:
I have files like a.txt, a.ascii, b.txt, b.ascii, b.mat.
I want to make folder first in the name of a and b, then I want to move files a.txt,a.ascii to folder a and b.txt,b.ascii,b.mat to folder b.
I tried the code as follows. However I need an automatic way to make folder and move the files to it.
#!/bin/sh
mkdir a b
for file in $(<list.txt)
do
cp "$file" a
done
Seems that this Bash script can do the job
#!/usr/bin/env bash
for file in $(<list.txt); do
dirn="${file%.*}"
mkdir -p "$dirn"
cp "$file" "$dirn"
done

Failed to copy list of files to another folder

I have a text file called "list.txt" that contain all the directories of the files that need to be copied to a new folder (dir_newfolder). I wrote the code like below:
for file in $(cat list.txt); do cp ${file} dir_newfolder; done
I got list of errors: cp:"file_name":No such file or directory. The file_names are the lines pulled out from the "list.txt". But when I copy each file_names from the error message and use cp to copy to the new folder. There is no error.
I am using mac os terminal.
Thanks in advance.
Copy a file or folder locally
In the Terminal app on your Mac, use the cp command to make a copy of a file.
For example, to copy a folder named Expenses in your Documents folder to another volume named Data:
% cp -R ~/Documents/Expenses /Volumes/Data/Expenses
The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.
in your case:
make sure you are providing correct path list.txt and the correct path for destiny folder, also i mentioned how to access file variable in double quotes , try this code it's working for me
for file in $(cat ~/Documents/list.txt); do cp "$file" ~/dir_newfolder; done

I want to move files in Shell

I have two folder xyz and Bin. I want to move xyz directory and it's sub directory as well to Bin folder.
I want output like this:
Bin/xyz/q.html
/images/0.gif
How can do move files and subdirectory and I will get that type of output.
Move (mv) can be thought of as rename. If mv is used to move a file from one drive to another, then a copy will be made and the original will be deleted.
If the file is a directory (folder), then all of the contents of that directory are also moved.
$ mv -v xyz Bin
moves the file called xyz, no matter what kind of file it is, even if it is a directory, into the existing directory called Bin. If Bin does not exist, then the move command will rename xyz to Bin.
If you only want to move a few files, for example, Bin/1.html is to be moved to Bin/xyz/1.html, then file being moved is a file called 1.html (not a directory called bin or xyz).
$ mv -v Bin/1.html Bin/xyz/1.html
will perform the move if the Bin/xyz exists and is a directory. If directory Bin/xyz does not exist, then this mv command will error because mv does not create parent destination directories.
$ mkdir -v Bin/xyz
will create an empty directory called xyz in an existing directory called Bin. Then files can be moved into the existing Bin/xyz directory.
The -v (--verbose) tells mv and mkdir to report what happened. The output that they produce given a success is what -v does. The most common way to list the content of a directory is ls, but the indentation that you desire will require some programming.
$ man mv
$ man mkdir
will show all the features of the mv an mkdir commands.

cp command in macOS doesn't create a new folder if it doesn't exist

cp -R /Desktop/One.java /Desktop/Nine
The folder Nine is not created. Instead, a new file Nine is created without any extension.
If cp is used with -R, then the target (/Desktop/Nine) is created with the same file type as the source (regular file). If the source is a directory (it isn't), the full directory hierarchy will be copied.
In your case, the source is a single regular file, in which case cp -R will act identically as cp (with no -R).
If you want to copy the file /Desktop/One.java into a new directory /Desktop/Nine:
mkdir /Desktop/Nine
cp /Desktop/One.java /Desktop/Nine

Copying from mount share drive to local folder through script

This is my first time trying to work with Linux Scripts so this may be something obvious.
Here is what I am trying to do:
Remove all contents from local folder - rm /home/user/Documents/Exercise/
Copy files from a shared windows network drive - cp smb://server/arc/Exercise%20Files/Word/
So from my understanding my command should look like this
rm /home/user/Documents/Exercise/
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
But anytime I try and run either of the above commands I get the following error:
"rm: cannot remove `/home/user/Documents/Exercise/': Is a directory"
"cp: cannot stat `smb://server/arc/Exercise%20Files/Word/': No such file or directory"
What am I doing wrong?
Kind Regards,
M
Based on your request and your test, let me point what is not written properly:
Remove all contents from local folder
rm /home/user/Documents/Exercise/
Error says rm: cannot remove /home/user/Documents/Exercise/': Is a directory
You should
rm /home/user/Documents/Exercise/*
which will delete everything inside the directory, but not the directory.
Copy files from a shared windows network drive
cp smb://server/arc/Exercise%20Files/Word/ /home/user/Documents/Exercise/
Error says cp: cannot stat smb://server/arc/Exercise%20Files/Word/': No such file or directory
You should check if route smb://server/arc/Exercise%20Files/Word/ is correct. Then, use the following:
cp smb://server/arc/Exercise%20Files/Word/* /home/user/Documents/Exercise/
You can't delete a directory if it has content within it.
To delete the content and the directory at the same time, use the following command:
rm -r /home/user/Documents/Exercise/
This recursively deletes the directory and any content within it.
To copy the file, I believe you have to mount the directory beforehand, like so:
mount -t cifs //server/share /mnt/mount_directory -o user=username
Can you confirm if that works?
Remove / Delete Command:
rm -rfv /home/user/Documents/Exercise/*
Copy Command:
cp -rfv /home/user/Documents/ExerciseShare/ExerciseFiles/Word/ /home/user/Documents/Exercise/

Resources