Any way to "alias" file patterns for use in a command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Any way to "alias" file patterns for use in a command?
# 1  
Old 02-18-2013
Question Any way to "alias" file patterns for use in a command?

First, I apologize for my 'noobness' with Linux and the shell. I'm running Ubuntu with zsh as my shell.

What I'd like to be able to do is clean up a messy Downloads folder by moving categories of files to different directories with something like:
Code:
mv dir/$vids dest
mv dir/$music dest
mv dir/$pics dest

where the variable would expand to a pattern for those types of files before execution.

I tried setting pics='*.(#i)(jpg|jpeg|gif|png|bmp)' but that didn't work. So is there any way to do something like this or do I have to write my own function? It just seems like a real pain to write a function for every instance where I'd like to manipulate certain types of files. Maybe I'd like to copy all pictures, or list only movies in a directory, or find all music, etc.
# 2  
Old 02-19-2013
Code:
 
pics="[a-z]*.{bmp,png,gif,jpeg,jpg}"
eval ls $pics| xargs -i mv {} dst\;

# 3  
Old 02-19-2013
Be careful with eval. Could be dangerous if the pics variable is built from user input. Try this:

Code:
pics="[a-z]*.{bmp,png,gif,jpeg,jpg};who" 
eval ls $pics

# 4  
Old 02-19-2013
Thanks for the replies. So I take it there is no easy way of basically using an alias in the middle of a command? I find it hard to believe that no shell would come up with a syntax for it, unless there are potential problems that I'm not thinking of.

If anyone feels like critiquing my attempt at writing a function to do this, I wouldn't mind. I imagine I could expand this to just a function like "pics" and then accept parameters for copy/move/list/etc.

Code:
mvpics() {
  if [[ -d $2 && -d $1 ]] ; then
    mv $1/*.(#i)(jpg|jpeg|gif|png|bmp) $2
  elif [[ -d $1 && $# == 1 ]] ; then
    mv ./*.(#i)(jpg|jpeg|gif|png|bmp) $1
  else
    echo 'mvpics <origin> <destination> or mvpics <destination>'
  fi
}

# 5  
Old 02-19-2013
You probably need some checks, and error reporting if no pics exist in origin.

I'm not a big zsh user so unsure how zsh handles expansion when no match is found.
# 6  
Old 02-19-2013
use gui and go to the root folder and seek your files

Code:
root/

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. UNIX for Dummies Questions & Answers

Calling Patterns of ".*" from a file

I used to create a file to read my patterns and call them with grep but now I want to use different sort of patterns and it does not work with a list on a accept file Grep –F –f acceptfile.txt mainfile.txt > outputfile.txt I have a data like this as they are in sequence of 5 The box of... (8 Replies)
Discussion started by: A-V
8 Replies

4. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. UNIX for Dummies Questions & Answers

Alias, function or script (bash) to "revert" cd command?

In all of my brief and superficial experience with Unix or Linux, the one curious and consistent thing has been that 'cd ./' (back up one directory level) has done absolutely nothing in any of them. Now I understand that, at least for bash, 'cd ./' appears to have been substituted by 'cd ..' Am... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

10. UNIX for Advanced & Expert Users

All alias in .profile lost when "script" command is called

Hi, I was trying to call "script <an ip add>" command from .profile file to log everything whenever anyone logs in to this user. I did the following at the end of .profile. 1) Extracted the IP address who logged in 2) Called script < ip add> . The problem I am facing is all, aliases etc. written... (3 Replies)
Discussion started by: amicon007
3 Replies
Login or Register to Ask a Question