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.

4
  • 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)." Commented Apr 13, 2015 at 6:18
  • possible duplicate of Make bash alias that takes parameter? Commented Apr 13, 2015 at 6:36
  • Maybe use csh instead of bash so you can do this the way you want? stackoverflow.com/questions/941338/… Commented Apr 13, 2015 at 6: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. Commented Apr 13, 2015 at 7:07

3 Answers 3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

You should double-quote the $variable like "$variable" to prevent globbing and word splitting.
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.

Comments

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.