TOPIC: UNIX SHELL
Smarter file renaming using PowerShell
14th November 2014It appears that the Rename-Item commandlet in PowerShell is a very useful tool when it comes to smarter renaming of files. Even text substitution is a possibility, and what follows is an example that takes the output of the Dir
command for listing the files in a directory and replaces hyphens with underscores in each one.
Dir | Rename-Item –NewName { $_.name –replace “-“,”_” }
The result is that something like the-file.txt becomes the_file.txt. This behaviour is reminiscent of the rename
command found on Linux and UNIX systems, where regular expressions can be used, like in the following example that has the same result as the above:
rename 's/-/_/g' *
In both cases, you do need to be careful as to what files are in a directory for this, though the wildcard syntax on Linux or UNIX will be more familiar to anyone who has worked with files via almost any command line. Another thing to watch in the UNIX world is that *
parses the whole directory structure, and that could be something that is not wanted for much of the time.
All of this is a far cry from the capabilities of the ren
or rename command used in the days of MS-DOS and what has become the legacy Windows command line. Apart from simple renaming, any attempt at tweaking a filename through substitution ended up with the extra string getting appended to filenames when I tried it. Thus, the PowerShell option looks better in comparison.
Turning on autocompletion for the bash shell in terminal sessions
26th June 2013At some point, I managed to lose the ability to have tab-key-based autocompletion on terminal sessions on my Ubuntu GNOME machine. Wanting it caused had me to turn to the web for an answer, and I found it on a Linux Mint forum; the bash shell is so pervasive in the UNIX and Linux worlds that you can look anywhere for a fix like this.
The problem centred around the .bashrc
file in my home area. It does have quite a few handy custom aliases, and I must have done a foolish spring-clean on the file sometime. That is the only way that I can explain how the following lines got removed:
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
What they do is look to see if /etc/bash_completion can be found on your system and to use it for tab-based autocompletion. With the lines not in .bashrc
, it couldn't happen. Others may replace bash_completion with bash.bashrc
to get a fuller complement of features, but I'll stick with what I have for now.
Filename autocompletion on the command line
19th October 2007The Windows 2000 command line feels an austere primitive when compared with the wonders of the UNIX/Linux equivalent. Windows XP feels a little better, and PowerShell is another animal altogether. With the latter pair, you do get file or folder autocompletion upon hitting the TAB key. What I didn't realise until recently was that continued tabbed cycled through the possibilities; I was hitting it once and retyping when I got the wrong folder or file. I stand corrected. With the shell in Linux/UNIX, you can get a listing of possibilities when you hit TAB for the second time and the first time only gives you completion as far as it can go with certainty; you'll never get to the wrong place, though you may not get anywhere at all. This works for bash, but not ksh88 as far as I can see. It's interesting how you can take two different approaches to reach the same end.