Copying from mount share drive to local folder through script - bash

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/

Related

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

rm -r -f doesn't delete inner folder

I have large projects and some scripts to compile them. I can't add all code here, so I'll try to simplify the problem: in the cleaning part, I need to clean folder named directory which contains other directory named innerDir. I have this bash command for cleaning directory:
clean:
rm -r -f directory
When directory is a folder that I created with mkdir -p beforehand. When I clean, I get this error:
rm: cannot remove 'directory': Directory not empty
But when I try to enter directory , I see that it's empty. So for debugging, I modified my cleanning part to be:
rm -r -f directory/*
find directory
rmdir directory
(it's suppose to do the same, but here I also get the chance to see if all the content of directory was really deleted).
Now I get this error:
find: 'directory/innerDir': Permission denied
There are two things that unclear for me here:
(1). innerDir was created with makedir -p before the clening part, without any change to the permissions of it later in the code. Why don't I have permission to delete it?
(2). If I try to clean again- the cleaning succeed and I don't have any permission problem. So, if I got permission error in the first time I tried to delete it, why don't I get it in the second time?
If your permissions are valid down the directory tree, rm -fr directory ought to work.
If you don't have read access on innerDir, then is it possible/likely (depending on running processes, perhaps) that something has written to innerDir, but the file gets cleaned up after so that the directory becomes free?
Can you give examples of permissions, ownership, and some scope of the operations happening between each step?
Could you rename the parent folder while working, and/or lock it's permissions to prevent other users or processes from altering things?

How to delete the files present in a directory

I have a folder in my directory called input_files which contains the list of input files. I am trying to delete the contents of this folder using the below command but getting I'm getting an error.
find /u/users/kisri1/scripts/design_matrix/input_files* -type f -delete
Error:
/u/users/kisri1/scripts/design_matrix/input_files: Is a directory
Please help to solve this thing. I want to write a shell script which can access the folder and delete the files.
rm is perfect in your case.
rm -rf /u/users/kisri1/scripts/design_matrix/input_files/* would do the trick and delete all files in your input_files directory without discrimination.
use the lines:
cd /u/users/kisri1/scripts/design_matrix/input_files
rm *
this will delete all files in the directory

Bash: duplicate + rename folder

Suppose I have a folder named my_folder_old in /path/to/folder, how can I create a duplicate named my_folder_new in the same directory?
EDIT
Moreover if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?
Tutorial copy files, folder link: link
Manual cp command : Link
cp -frp /path/to/folder/my_folder_old -T /path/to/folder/my_folder_new
-f, --force
if an existing destination file cannot be opened, remove it
and try again (this option is ignored when the -n option is
also used)
-p same as --preserve=mode,ownership,timestamps
-R, -r, --recursive
copy directories recursively
-T, --no-target-directory
treat DEST as a normal file
Though if my_folder_new already exists, my_folder_old is created inside the first and not substituted. Why is this happening?
The reason why is this happening because, my_folder_new already created. Doing same cp command it will see as new path, /path/to/folder/my_folder_new/
I was dealing with this same issue, was going crazy ahaha, I tried cp -frp but did not work, so, before of going to do cp just remove the existing folder using rm, see below more info about this:
Remove Directory Linux
If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:
rm -rf dir1

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

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

Resources