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

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.

Related

How can I copy multiple files in the same directory with different names but same extension in bash?

The requirement is to make copies of n(n>10000) files in the same linux directory.
The extension of the files has to be intact and can probably add numbers to distinguish among files.
For e.g. if one file is text1.txt the other could be text2.txt
But I have to create multiple copies from multiple files and not from a single file.
Please help.
Bash pattern substitution might help you here. If you e.g. want to copy all .txt files, you can do it like this:
for file in *.txt # add any other name wildcards
do
filename=${file%.*} # removes everything after the last dot
extension=${file##*.} # removes everything before the last dot
cp "$file" "${filename}-copy.${extension}" # adds the -copy suffix to every copy
done
You might want to look into tools like logrotate which can take for example a glob and rotate each of the files on a regular basis.

copy and replace file in tcl

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

shell script : appending directory path and filename

I want to copy a file from a directory using shell script
Suppose I save the directory and file name seperately as
dir=/home/user/directory/
file=file_1
to copy the file Im using this command in my script
cp $dir$file .
But I get this error
/bin/cp omitting directory '/home/user/directory'
I have tried all combination eg. omitted the trail backslah from variable dir, etc but nothings working. I cant understand what is wrong with this code. Pleas help
Maybe the command $dir$file is not getting unpacked in the shell (ie only the directory variable is getting unpacked, not the file variable)!!!!!
It looks like you are having problem with expansion in cp $dir$file . In order to prevent possible problems, it is better to protect your variable with braces and double quote the full path/file to make sure you don't get caught by spaces in either the filename or heaven forbid the user's dirname:
cp "${dir}${file}" .
This will prevent the possibility the second $ is missed. Also make sure you have read access to other users /home (if you are root or using sudo you should be fine)
If you see this, when you somehow assign an empty string to file somewhere. Search your script for file= and unset file.
You can also debug this by adding
echo ".${file}."
in the line before the cp command. I'm pretty sure it prints .., i.e. the variable is empty or doesn't exist.

renaming files with strange unicode names

Recently I downloaded some files from a website, but their names contain strange unicode characters, which my console doesn't show them properly. Now I want to rename these files to be able to use these files, but I get the following error:
mv: cannot stat`FILENAME': No such file or directory
But I am sure that these files exist.
I wonder how I can rename these files, properly.
Any ideas?
Using globbing characters (like ? or *): mv *some-typeable-and-unique-substring* ...
Using the tab-completion of your favourite shell: your start typing mv, then the beginning of the filename, then you press TAB, and then you can enter the second parameter.
If there are other files in that directory, you might have to move them to another directory to be able to use the tab-completion or the wildcards.

One-line copy command when source and dest path are the same

I want to backup a file in some-other sub-directory different from my current directory like this:
cp /aaa/bbb/ccc/ddd/eeee/file.sh /aaa/bbb/ccc/ddd/eeee/file.sh.old
As you see both source and dest dir are the same, so common convention would be to change to the common directory, perform the copy im ./, then change back to the original directory.
Is there a single-line command to accomplish the copy in this situation?
Yes. Use this:
cp /aaa/bbb/ccc/ddd/eeee/{file.sh,file.sh.old}
The curly braces will cause the first part of the string to be reused for each of the items separated by commas. Bash is what expands the above into two separate paths and then passes it to cp. To see what Bash would be passing to cp, simply add an echo to the beginning:
echo cp /aaa/bbb/ccc/ddd/eeee/{file.sh,file.sh.old}
You will see that produces your original statement:
cp /aaa/bbb/ccc/ddd/eeee/file.sh /aaa/bbb/ccc/ddd/eeee/file.sh.old
You're just using a Bash trick to save on typing.

Resources