Failed to copy list of files to another folder - macos

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

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

Copy command in Mac Terminal fails with "No such file or directory"

This should be straightforward, but I'm getting weird results.
I have a folder with subfolders containing ~4000 files. I'm trying to copy just the files of a certain filetype to another folder, while preserving the subfolder hierarchy.
Command:
cp -R /Users/Steve/Desktop/Hardscapes/*.LOB /Users/Steve/Desktop/Temp
fails with the message:
"/Users/Steve/Desktop/Hardscapes/*.LOB: No such file or directory".
I created the command by typing cp -R then dragging the source folder to the terminal window, adding *.LOB after the /, and dragging the destination folder to the terminal window.
Troubleshooting:
replacing *.LOB with *.* gives the same error.
cp -R /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp copies the entire Hardscapes folder to Temp, with all its subfolders and files.
Thanks for your help and suggestions.
EDIT: The folder Hardscapes contains only other folders. If I run the command above using one of those folders as the source, the contents are copied faithfully. The folder Hardscapes itself contains no .LOB files - they're only in the subfolders.
So maybe that's the issue, cp can't find any files corresponding to Hardscapes/*.LOB? But I thought the -R switch was supposed to tell it to look through all the subfolders.
Next I put a file named Test.LOB in the Hardscapes folder. The command above copies only that file and none of the subfolders. It looks like the -R switch is not doing its job. Do I have the syntax right?
Try this:
rsync -a --prune-empty-dirs --include '*/' --include '*.LOB' --exclude '*' /Users/Steve/Desktop/Hardscapes/ /Users/Steve/Desktop/Temp
As you already mentioned, directory Hardscapes itself contains no .LOB files. That's why your mask /Users/Steve/Desktop/Hardscapes/*.LOB results in matching no files at all.

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

Bash script to archive sub-folders with 7zip without archiving archives

I am trying to write a bash script that will zip all sub folders into .cbz files, while leaving the top-level folders alone and that doesn't archive already zipped folders using 7zip.
Here is what I wrote:
#!/bin/bash
for folder in /home/angelucifer/Documents/Personal/MangaLib/*/*
do
7z a -mx0 -mmt2 -tzip "${folder%/}.cbz" "$folder"
rm -rf "$folder"
done
My current issue seems to be that it will archive already zipped folders, but everything else works fine.
The reason I specified a directory is to avoid accidentally archiving the contents of my home folder... again.
My intent for this script is to go into my MangaLib folder, and archive the contents of the folders inside it, without archiving those folders too, which is the purpose of the two wildcards in the address. Then, it should delete the original folder and leave only the .cba file.
Again, the issue is that I will routinely run this script to compress the subfolders of any new additions to my MangaLib folder, but the script will also compress the previously archived folders as well, which is not my intent.
Use file command to find file type. Then zip only the ones you need.
#!/bin/bash
for folder in /home/angelucifer/Documents/Personal/MangaLib/*/*
do
if ! `file $folder | grep -i zip > /dev/null 2>&1`; then
##Most of the zip utilities contain "zip" when checked for file type.
##grep for the expression that matches your case
7z a -mx0 -mmt2 -tzip "${folder%/}.cbz" "$folder"
rm -rf "$folder"
fi
done

Copying files from one folder to another using a shell script

I have a script to copy the files from one location to another where i am passing the first location as parameter to the script
#!/bin/bash
locatn=$1
echo $locatn
cp -r /locatn/ /ws/priyapan-rcd/workspace/automation/
but when i run this code this throws error as
cp: cannot stat `locatn': No such file or directory
what could be the issue
Formatting looks a bit weird but as #Patick Trentin said you simply forgot a $ making your script always copy the files to the same location ignoring the given parameter.
#!/bin/bash
locatn=$1
echo $locate
cp -r /${locatn}/ /ws/priyapan-rcd/workspace/automation/

Resources