copy and replace file in tcl - linux

I am using the following tcl command:
file copy ?-force? file1 file2
here 'file1' and 'file2' are text files having same names, I want to copy file1 from the location by moving up the parent directory and replace the file2 located in the current directory. So I want to perform something like this:
step1: cd ../../
step2: copy 'file1.txt' from the step1 location
step3: now move to the current directory
step4: replace 'file2.txt' with 'file1.txt'
I don't know how to mention the path in the 'file copy' command ? It would be also better if you mention the shortcut to navigate like in step1 but for a longer path. So I can skip writing manually a longer path. Thank you.

file copy -force ../../file1.txt file2.txt
You can't copy a file like you do in a GUI. The file copy command immediately creates a copy of the source file in the target location. Both the source and target arguments are file names (or possibly a directory name for the target) including full paths, so you simply join up the path with the base file name.
I'm not sure what you mean by "shortcut to navigate". The command for changing the current directory is cd, with the path to a directory as argument. But, again, you don't need to change directory to copy a file.
Documentation: cd, file

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}" ../

Use of mv comand in Linux

I have a bunch of files
that have different extensions:
.png .fa .sh
which were in a directory called 'files'.
I wanted to move each
.png to images directory
.fa to fasta directory
.sh to upper directory Golden_Asian.git/
Moving the .png and .fa files worked well,
but for the .sh file, when I did
mv files/*.sh Golden_Asian.git
it just made type POSIX shell script, ASCII text executable, with the name
Golden_Asian.git
the name of the original file was
script.sh
so I was supposed to get a result like
Golden_Asian.git/script.sh
Did I do something wrong or is it the right result?
The original file in the "files" directory has disappeared, so I'm pretty sure
the file has been transferred to the right directory but don't understand why the name has been changed.
It's the right result if there was only one .sh file and there was no directory called Golden_Asian.git in the current directory. You executed mv files/script.sh Golden_Asian.git, so it moved the file from the original name to the name you specified. If there was already a file Golden_Asian.git in the current directory, it would be clobbered (replaced) by the new file. If there was a folder named Golden_Asian.git in the current directory, you'd have a file Golden_Asian.git/script.sh after the command. That's not what you got, so it is legitimate to deduce that you did not have a folder called Golden_Asian.git in the current directory when you executed the command.
If you'd specified:
mv *.sh Golden_Asian.git/
then it would have required the directory to exist and the result would have been a file Golden_Asian.git/script.sh. If you'd had more than one .sh file, then you'd have needed the directory Golden_Asian.git to exist. You said "upper directory"; did you intend to write:
mv *.sh ../Golden_Asian.git
If there was a directory in the parent directory called Golden_Asian.git, then the script would have been moved there. If there was no directory, the file would have been moved up a level and renamed. Again, a trailing / would have prevented confusion.
Note that moving a file into a GIT directory (especially a bare repository) is probably not a good idea.

Copy or move with combining source and destination path to avoid long path repeated

I need to edit many python files. When I about to start editing a file, I just create a copy of the file and will compare to this copy after I am finished editing and save changes to original file.
So I tend to work from one fixed location/path and edit/copy files in different paths by using their absolute path. I end up providing complete path for source file and complete path for destination file.
How can I use unix cp command which avoids mentioning path twice when both files are to be in same directory/path.
I have tried the traditional copy command: cp source-file target-file. But I had to repeat the path twice. For example:
cp /main/dept_1/class_2/get_list.py /main/dept_1/class_2/copy_get_list.py
There is a different way to try this but I forgot exact syntax, but it goes this way:
cp /main/dept_1/class_2/get_list.py[copy_get_list.py]
I expect to mention the path only once and be able to provide source and destination file names in copy (cp) command.
$home:ls /main/dept_1/class_2/
get_list.py
$home:cp /main/dept_1/class_2/get_list.py[copy_get_list.py]
I get error: "cp: missing destination file operand after"
Use curly braces:
cp /main/dept_1/class_2/{,copy_}get_list.py
When you have a brace list in a word, the word is repeated with each list element replacing the brace list.

How do I copy differing content files from one directory to another?

There exists two directories: a/ and b/.
I'd like to copy all the files(recursively) from a/ into b/.
However, I only want to copy over an a file if its content is different than the already existing b file. If the corresponding b file does not exist, then you would still copy over the a file.
*by "corresponding file", I mean a files with the same name and relative path from their parent directories.
note:
The reason I don't want to overwrite a b file with the same exact contents, is because the b directory is being monitored by another program, and I don't want the file date to change causing the program to do more work than required.
I'm essentially looking for a way to perform a cp -rf a/ b/ while performing a diff check on each file. If the file's are different, perform the copy; otherwise skip the copy.
I see that cp has an update flag:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the
destination file is missing
but this will not work because I'm not concerned about newer files; I'm concerned about different file contents.
Any shell language will do.
I've been attempting to get this to work by injecting my diff check into a find command:
find a/ ??? -exec cp {} b \;
This doesn't seem like an uncommon thing to do between two directories, so I'm hoping there is an elegant command line solution as aposed to me having to write a python script.
You can achieve this using rsync. Files or directories will be updated only if there is any new update in source folder.
$rsync -av --progress sourcefolder destinationfolder

Bash, copy files from folder to another

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.

Resources