I am trying to simplify my work with the help of Alias commands in my bash shell.
Problem Statement: I want to copy different files from different directories to one single folder. The syntax i am using here is as below
cp <folder>/<file> <path>/file.dir
Here I want to save the destination file with filename.directory for easy identification. To achieve the same, I have written the below alias.
Alias Script
cp $Folder/$fileName ~/<path>/$fileName.$Folder
OR
cp $1/$2 ~/<path>/$2.$1
Expected output,
cp bin/file1 ~/Desktop/personal/file1.bin
cp etc/file2 ~/Desktop/personal/file2.etc*
However, It's failing at parsing the source file. i.e. $Folder
is not replaced with my first argument.
cp: cannot stat `/file1': No such file or directory
I am writing the above script only to reduce my command lengths. As I am not expert in the above code, seeking any expert help in resolving the issue.
A directory contains some files say ~/Documents/file1.d contains newfile.txt
Add the variable 'file' in .bashrc for example my .bashrc is shown here
Now whenever you copy to '$file' it will copy to file1.d directory under ~/Documents :)
Rather than using an alias you could use a function which you define in some suitable location such as .profile or .bashrc
For example:
YAMC: Yet another myCp
This will work approx like
cp
as last argument is destination dir:could be used in that way:
To test this, you could replace
cp -i
byecho "cp -i"
.More sophisticated "
myCp
"This will take last directory in source path as extension to copy files:
There is a
-d
option to don't copy anything, but show result command:or
There is no way a bash
alias
can use arguments as you are trying to do. However, perl basedrename
can probably help you here. Note that it will effectivelymv
the files, notcp
them.Limitations: You can only process the files in 1 sub-directory level.
So, below alias can work (with above limitation):
You can make more sophisticated perl expression if you want to work with multi-directory-level file structure.