There's tons of rename examples out there, but most of the time, these are for Unix/Linux users. What about if I want to use it using windows?
First, go to https://strawberryperl.com/, then install the appropriate build (64 or 32 bits).
Now you have Perl and some utilities, but not the rename command.
To install it, open a cmd.exe terminal, then
cpan File::Rename
Now, the command is accessible.
file-rename
Usage:
rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -u [enc]]
[ -e|-E perlexpr]*|perlexpr [ files ]
A guide with some examples of using this utility: Perl's rename
To use the quoting style of 'Linux' instead of "DOS", better use powershell to use the rename command line with 'single quotes'.
Related
I want to see a list of all possible options for a specific command line tool. The --help option can be useful, but might not include all the options and sometimes you just need a list instead of a detailed description. Basically the bash equivalent of dir() in python.
For example:
The command python --help pulls up a description of options/arguments. However it doesn't include options such as python --version. Does bash have a way to check this?
It depends on what you're trying to get help on.
For an executable like python your best bet is the man page. This is only really available on Unix though. eg.
man python
This brings up a screen that looks like:
PYTHON(1) General Commands Manual PYTHON(1)
NAME
python - an interpreted, interactive, object-oriented programming lan‐
guage
SYNOPSIS
python [ -B ] [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ]
[ -O ] [ -OO ] [ -R ] [ -Q argument ] [ -s ] [ -S ] [ -t ] [ -u ]
[ -v ] [ -V ] [ -W argument ] [ -x ] [ -3 ] [ -? ]
[ -c command | script | - ] [ arguments ]
DESCRIPTION
Python is an interpreted, interactive, object-oriented programming lan‐
guage that ...
COMMAND LINE OPTIONS
...
-V , --version
Prints the Python version number of the executable and exits.
...
This uses a program called less to display the information. You can scroll with the arrow keys and quit with q. There are more commands available in less. Type man less to find out more.
If you're looking for help on a bash builtin (eg. echo) try the bash builtin help eg.
help echo
On an old Solaris that only has plain bourne shell, I haven't been able to correctly translate a simple test such as:
[ -d '/export/home/mydir' -o ! -e '/export/home/mydir' -a -d $(dirname '/export/home/mydir') ]
...as is supported under modern POSIX shells such as ash, bash, ksh, &c.
Any suggestions please?
With portable shell scripting, try avoiding using -a and -o for and and or. Additionally, as others have commented, $() expansion may not be available, but backticks will be.
Try this instead:
if [ -d '/export/home/mydir' ] || [ ! -e '/export/home/mydir' ] && [ -d `dirname '/export/home/mydir'` ]; then
If [ isn't available, you may need to use test instead of [ and remove the closing ]s.
For reference, here is a guide on writing portable shell scripts.
I was looking at how to use runit to run gunicorn. I was looking at the bash file and I don't know what -f $PID does in
#!/bin/sh
GUNICORN=/usr/local/bin/gunicorn
ROOT=/path/to/project
PID=/var/run/gunicorn.pid
APP=main:application
if [ -f $PID ]; then rm $PID; fi
cd $ROOT
exec $GUNICORN -c $ROOT/gunicorn.conf.py --pid=$PID $APP
Google is useless in this case because searching for flags is useless
Google is useless in this case because searching for flags is useless
Fortunately, the Bash Reference Manual is available online, at http://www.gnu.org/software/bash/manual/bashref.html. It's the first hit when you Google for "Bash manual". §6.4 "Bash Conditional Expressions" says:
-f file
True if file exists and is a regular file.
-f - file is a regular file (not a directory or device file)
Check this out for all file test operators:
http://tldp.org/LDP/abs/html/fto.html
The [ is the same as the command test which allows you to test certain things. Try help test to find out what the flags are. Things to be careful with are spaces - the [ needs a space after it.
-f checks if the file exists and is a regular file.
[ -f "$var" ]
Checks if $var is an existing file (regular file). Symbolic link passes this test too.
I came across this shell script for OSX and I don't understand the following:
[ -e ~/Library/Application\ Support/Dock/*.db ] on line 191?
What is that command doing? I've also seen things like [ -r "$file" ]. I don't completely understand this syntax.
-e is an argument to [ command (which is an alias of test; you can read the full docs with man test). It means «if the file or directory exists». This line is harmless unless [ was redefined earlier.
Like most makefiles, I have some bash scripts that use variables for executables. For example, $mysql_exec for /usr/bin/mysql. I would like to be able to say something like:
mysql_exec=mysql
to use $PATH or
mysql_exec=/usr/bin/mysql
to get an absolute location without $PATH. I also want to check to see if the executable is valid using something like this:
if [ ! -x $mysql_exec ] ...
However, this command:
if [ ! -x "mysql" ]; then print "oh oh"; fi
Actually prints "oh oh", even though mysql is in my $PATH. From the command-line, typing mysql has the same effect as typing /usr/bin/mysql. So, how do I get bash really check to see if $mysql_exec is an executable ($PATH and all)?
In Bash, you can use the built-in type -P to force a resolution against the PATH or type -p to show the path only if there's no alias or function by that name. Using type avoids calling an external such as which.
Something like this may do what you're looking for:
[ -x "$(type -p "$mysql_exec")" ]
whether you use
mysql_exec=mysql
or
mysql_exec=/usr/bin/mysql
which is a small program that checks $PATH for a program specified as an argument:
$ which mysql
/usr/bin/mysql
Another useful utility (that isn't installed on all systems but is included in GNU CoreUtils) is readlink. readlink can give you back a full and absolute path, without symlinks. For example:
$ cd ~me/bin
$ ln -s `which mysql` mysupersql
$ readlink -f mysupersql
/usr/bin/mysql
I often use a combination of both so I know that the path is both absolute and not a symlink:
mysql_exec=$(readlink -f `which mysql`)
if [ ! -x "$mysql_exec" ] ; then
...
To add to Kaleb's answer, you an also check if the file is a symlink using if [-L $file] and follow that symlink if you can't install readlink. The rest of the check though remains as the way Kaleb mentioned.