Batch renaming files with Bash
You could use bash’s parameter expansion feature for i in ./*.pkg ; do mv “$i” “${i/-[0-9.]*.pkg/.pkg}” ; done Quotes are needed for filenames with spaces.
You could use bash’s parameter expansion feature for i in ./*.pkg ; do mv “$i” “${i/-[0-9.]*.pkg/.pkg}” ; done Quotes are needed for filenames with spaces.
Much simpler: use sudo to run a shell and use a heredoc to feed it commands. #!/usr/bin/env bash whoami sudo -i -u someuser bash << EOF echo “In” whoami EOF echo “Out” whoami (answer originally on SuperUser)
If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of sed, the -i option expects an extension argument so your command is actually parsed as the extension argument and the file path is interpreted as the command code. Try adding the -e argument … Read more
Another option, if portability is not your main concern, is to use associative arrays that are built in to the shell. This should work in bash 4.0 (available now on most major distros, though not on OS X unless you install it yourself), ksh, and zsh: declare -A newmap newmap[name]=”Irfan Zulfiqar” newmap[designation]=SSE newmap[company]=”My Own Company” … Read more
As a followup to mouviciel’s answer, you could also do this as a for loop, instead of using xargs. I often find xargs cumbersome, especially if I need to do something more complicated in each iteration. for f in $(find /tmp -name ‘*.pdf’ -or -name ‘*.doc’); do rm $f; done As a number of people … Read more
[*] After each command, the exit code can be found in the $? variable so you would have something like: ls -al file.ext rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi You need to be careful of piped commands since the $? only gives you the return code of the last element … Read more
Here is another way to do it: #!/bin/bash # Read Password echo -n Password: read -s password echo # Run Command echo $password The read -s will turn off echo for you. Just replace the echo on the last line with the command you want to run.
Use grep to filter IP address from ifconfig: ifconfig | grep -Eo ‘inet (addr:)?([0-9]*\.){3}[0-9]*’ | grep -Eo ‘([0-9]*\.){3}[0-9]*’ | grep -v ‘127.0.0.1’ Or with sed: ifconfig | sed -En ‘s/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p’ If you are only interested in certain interfaces, wlan0, eth0, etc. then: ifconfig wlan0 | … You can alias the command in your .bashrc … Read more
*/ is a pattern that matches all of the subdirectories in the current directory (* would match all files and subdirectories; the / restricts it to directories). Similarly, to list all subdirectories under /home/alice/Documents, use ls -d /home/alice/Documents/*/
You have syntax error in your if condition, use this if condition: if [ “$EUID” -ne 0 ]; OR using [[ and ]] if [[ “$EUID” -ne 0 ]];