Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through alphabets in Bash

I want to mv all the files starting with 'x' to directory 'x'; something like:

mv path1/x*.ext path2/x 

and do it for all alphabet letters a, ..., z

How can I write a bash script which makes 'x' loops through the alphabet?

like image 890
behzad.nouri Avatar asked Sep 04 '11 15:09

behzad.nouri


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is if [- Z in bash?

In both cases, the -z flag is a parameter to the bash's "test" built-in (a built-in is a command that is built-into the shell, it is not an external command). The -z flag causes test to check whether a string is empty. Returns true if the string is empty, false if it contains something.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.


1 Answers

for x in {a..z} do     echo "$x"     mkdir -p path2/${x}     mv path1/${x}*.ext path2/${x} done 
like image 66
Kamil Dziedzic Avatar answered Oct 04 '22 04:10

Kamil Dziedzic