0

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,

  1. cp bin/file1 ~/Desktop/personal/file1.bin
  2. 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.

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 2
    From `man bash` under **ALIASES**, "There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below)." – user3439894 Apr 13 '15 at 06:18
  • possible duplicate of [Make bash alias that takes parameter?](http://stackoverflow.com/questions/7131670/make-bash-alias-that-takes-parameter) – anishsane Apr 13 '15 at 06:36
  • Maybe use csh instead of bash so you can do this the way you want? http://stackoverflow.com/questions/941338/shell-script-how-to-pass-command-line-arguments-to-a-unix-alias – userABC123 Apr 13 '15 at 06:57
  • @sndsnd: As someone who switched *from* csh *to* bash, I would not recommend this. Bash, unlike, csh, has functions, and they're much more powerful than aliases. – Keith Thompson Apr 13 '15 at 07:07

3 Answers3

1

Rather than using an alias you could use a function which you define in some suitable location such as .profile or .bashrc

For example:

mycp()
{
   folder=$1
   filename=$2

   if [ $# -ne 2 ]
   then
       echo "Two parameters not entered"
       return
   fi

   if  [ -d $folder -a -r $folder/$filename ]
   then
      cp $folder/$filename ~/playpen/$filename.$folder
   else
      echo "Invalid parameter"
   fi
}
fpmurphy
  • 2,464
  • 1
  • 18
  • 22
0

There is no way a bash alias can use arguments as you are trying to do. However, perl based rename can probably help you here. Note that it will effectively mv the files, not cp them.

rename 's|([^/]*)/(.*)|/home/user/path/$2.$1|' */*

Limitations: You can only process the files in 1 sub-directory level.

So, below alias can work (with above limitation):

$ alias backupfiles="rename 's|([^/]*)/(.*)|/home/user/path/\$2.\$1|'"
$ backupfiles */*

You can make more sophisticated perl expression if you want to work with multi-directory-level file structure.

anishsane
  • 20,270
  • 5
  • 40
  • 73
0

A directory contains some files say ~/Documents/file1.d contains newfile.txt

joe@indiana:~/Documents$ ls -l $file
total 1
-rw-r--r--   1 joe      staff          0 May  5 11:39 newfile.txt

Add the variable 'file' in .bashrc for example my .bashrc is shown here

alias ll='ls -la'
file=~/Documents/file1.d

Now whenever you copy to '$file' it will copy to file1.d directory under ~/Documents :)

Joe
  • 1
  • 1