Bash, copy files from folder to another - linux

I have a problem with an Uni OS Course assignment.
Basically the task says:
Deliver now a file for assessment. The content of the file is: one line, containing a command that
copies all files with prefix "2016", from directory "ExercisesOS" to directory "OSLab".
Consider the current directory to be "~" when writing such command.
I have already tried with that code:
cp /ExercisesOS/2016* /OSLab
but it performs me two error.
How can I write the correct command?

You probably want to copy from the directory you are working.
To check where you are working:
$ pwd
/home/userdir
To copy from your working directory:
$ cp ExerciseOS/2016* OSLab/

mkdir OSLab && cp /ExercisesOS/2016* OSLab
This solution would assume that the directory 'OSLab' isn't already created.

Related

Move files between directories using shell script

I'm new to linux and shell script in general. I'm using a distribution of Debian on the WSL (Windows Subsystem for Linux). I'm trying to write a very simple bash script that will do the following:
create a file in a directory (child-directory-a)
move to the directory it is in
move the file to another directory (child-directory-b)
move to that directory
move the file to the parent directory
This is what I have so far (trying to keep things extremely simple for now)
touch child-directory-a/test.txt
cd child-directory-a
mv child-directory-a/test.txt home/username/child-directory-b
The first two lines work, but I keep getting a 'no such directory exists' error with the last one. The directory exists and that is the correct path (checked with pwd). I have also tried using different paths (i.e. child-directory-b, username/child-directory-b etc.) but to no avail. I can't understand why it's not working.
I've looked around forums/documentation and it seems that these commands should work as they do in the command line, but I can't seem to do the same in the script.
If anyone could explain what I'm missing/not understanding that would be brilliant.
Thank you.
You could create the script like this:
#!/bin/bash
# Store both child directories on variables that can be loaded
# as environment variables.
CHILD_A=${CHILD_A:=/home/username/child-directory-a}
CHILD_B=${CHILD_B:=/home/username/child-directory-b}
# Create both child folders. If they already exist nothing will
# be done, and no error will be emitted.
mkdir -p $CHILD_A
mkdir -p $CHILD_B
# Create a file inside CHILD_A
touch $CHILD_A/test.txt
# Change directory into CHILD_A
cd $CHILD_A
# Move the file to CHILD_B
mv $CHILD_A/test.txt $CHILD_B/test.txt
# Move to CHILD_B
cd $CHILD_B
# Move the file to the parent folder
mv $CHILD_B/test.txt ../test.txt
Take into account the following:
We make sure that all the folders exists and are created.
Use variables to avoid typos, with the ability to load dynamic values from environment variables.
Use absolute paths to simplify the movement between folders.
Use relative paths to move files relatives to where we are.
Another command that might be of use is pwd. It will tell you the directory you are on.
with your second line, you change the current directory to child-directory-a
so, in your third line there is an error because there is no subdirectory child-directory-a into subdirectory child-directory-a
Your third line should be instead :
mv test.txt ../child-directory-b
The point #4 of your script should be:
cd ../child-directory-b
(before that command the current directory is home/username/child-directory-a and after this command it becomes home/username/child-directory-b)
Then the point #5 and final point of your script should be:
mv test.txt ..
NB: you can display the current directory at any line of your script by using the command pwd (print working directory) in your script, it that helps
#!/bin/sh
# Variables
WORKING_DIR="/home/username/example scripts"
FILE_NAME="test file.txt"
DIR_A="${WORKING_DIR}/child-directory-a"
DIR_B="${WORKING_DIR}/child-directory-b"
# create a file in a directory (child-directory-a)
touch "${DIR_A}/${FILE_NAME}"
# move to the directory it is in
cd "${DIR_A}"
# move the file to another directory (child-directory-b)
mv "${FILE_NAME}" "${DIR_B}/"
# move to that directory
cd "${DIR_B}"
# move the file to the parent directory
mv "${FILE_NAME}" ../

Files get copied and renamed but I also get errors

I have a script that reads a text file that has all the nodes listed in there:
node1
node2
node3
.
.
.
It creates a ".conf" file for each node in the /etc/icinga2/zones.d/master/hosts/new/ directory
Copies the content of the file name linux-template into each new conf file.
Everything worked as I expected, but I also get errors for each node:
Can anyone please help?
Thanks
This is my script:
#!/bin/bash
cd /etc/icinga2/zones.d/master/hosts/new
while read f; do
cp -v "$f" /etc/icinga2/zones.d/master/hosts/new/"$f.conf"
cp linux-template.conf "$f.conf"
chown icinga:icinga "$f.conf"
done < linux-list.txt
Once everything got copied, I get these errors below (for all the nodes, ie. node 1):
cp: cannot stat ‘node1’: No such file or directory
chown: cannot access ‘node1’: No such file or directory
It looks like it's complaining because there isn't a file called "node1" in your directory and you have verbose mode on.
This script looks like it will also cause undesired behavior if you're not located in the /etc/icinga2/zones.d/master/hosts/new/ directory when you run it.
The script is saying:
Copy files node1,node2,... in my current directory and place the
copy here: /etc/icinga2/zones.d/master/hosts/new/"$f.conf"
Copy linux-template.conf from the current directory and name it "node[1-9].conf" in the current directory.
Chown the "node[1-9].conf" in the current directory.
I suggest using absolute paths and I'm not quite sure why the first cp is necessary. If you're intending to copy linux-template.conf into each node[1-9].conf that you created in step 1, the second copy will create and overwrite the file anyway and step 1 would not be needed.

Cygwin copy directory with wrong name

I create a little script to copy a directory.
Sadly, cygwin do not copy the name of the directory, but instead copy all the content in a directory called ·(it's an intepunct, not a dot en.wikipedia.org/wiki/Interpunct ).
There are no problems to execute all the commands directly in the terminal!
Anyone has ideas?
this is the script:
cp -r //REMOTE-PC/folder1/folder2/folder-to-copy/ ./local-folder/folder-to-copy/
thanks

shell script mv is throwing unhelpful error "No such file or directory" even though i see it

I need to use a shell script to move all files in a directory into another directory. I manually did this without a problem and now scripting it is giving me an error on the mv command.
Inside the directory I want to move files out of are 2 directories, php and php.tmp. The error I get is cd: /path/to/working/directory/php: No such file or directory. I'm confused because it is there to begin with and listed when I ls the working directory.
The error I get is here:
ls $PWD #ensure the files are there
mv $PWD/* /company/home/directory
ls /company/home/directory #ensure the files are moved
When I use ls $PWD I see the directories I want to move but the error afterward says it doesn't exist. Then when I ssh to the machine this is running on I see the files were moved correctly.
If it matters the directory I am moving files from is owned by a different user but the shell is executing as root.
I don't understand why I would get this error so, any help would be great.
Add a / after the path to specify you want to move the file, not rename the directory.
You should try this:
mv $PWD/\* /home/user/directory/
Are your variables properly quoted? You could try :
ls "$PWD" #ensure the files are there
mv "$PWD"/* "/company/home/directory"
ls "/company/home/directory" #ensure the files are moved
If any of your file or directory names contains characters such as spaces or tabs, your "mv" command may not be seeing the argument list you think it is seeing.

Recursively copy contents of directory to all target directories

I have a directory containing a set of subdirectories and files. I need to recursively copy all the content of this directory to all the subdirectories of another directory, also recursively.
How do I achieve this, preferably without using a script and only with the cp command?
You can write this in a script but you don't have to. Just write it line by line in the terminal:
# $TARGET is the directory containing subdirectories where you want to STORE the copies
# $SOURCE is the directory containing the subdirectories you want to COPY
for dir in $(ls $TARGET); do
cp -r $SOURCE/* $TARGET/$dir
done
Only uses cp and runs on both bash and zsh.
You can't. cp can copy multiple sources but will only copy to a single destination. You need to arrange to invoke cp multiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.
The first part of the command before the pipe instruct tar to create an archive of everything in the current directory and write it to standard output (the – in place of a file-name frequently indicates stdout).
tar cf - * | ( cd /target; tar xfp -)
The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the cd and tar commands are contained within parentheses, their actions are performed together.
The -p option in the tar extraction command directs tar to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as superuser, this option is turned on by default and can be omitted.
Also you can use the following command, but it seems to be quite slower than tar;
cp -a * /target

Resources