How to use mkdir -v as defaul mkdir in Linux - bash

In linux ,when I use mkdir -v dir1 ; linux will show me some information like "dir1 create
" "dir1 exits" ;
mkdir -v make me fell a "Sense of Security" , i like it .
But I want make "mkdir -v " as the default "mkdir" , everytime I use mkdir , it will show me infomation ;
I think this idea beacause everytime i use "mkdir -v", i feel it unconvinient .
I have try some method , like export " alias mkdir="mkdir -v " " in my .bashrc , but it didn't work.
Any easy method to achieve this idea ? Thank you very much !

You will have to define the alias to do this
The command below will append to your .bashrc and will accomplish the desired result
echo 'alias mkdir="mkdir -v"' >> ~/.bashrc
Remember to get a new shell or type bash to reload your .bashrc file in your current shell
If once in a while you don't want this behavior just prefix your command with a \
\mkdir test
The above will ignore the effect of alias just for this one command.
Sample run
$ alias mkdir='mkdir -v'
$ mkdir test
mkdir: created directory 'test'
$ \mkdir test1
$

Related

How to handle additional files when building and running a AUR package?

So I was just playing around and created this simple Shell script:
TestScript.sh
#!/bin/bash
read -p "read or write? " INP
if [[ "${INP}" == "write" ]]
then
read -p "Write your text: " TEXT
touch /usr/share/textfile.txt
echo "${TEXT}" >> /usr/share/textfile.txt
else
cat /usr/share/textfile.txt
fi
which of course can easily read and write into a file under /usr after gaining sudo priviliges:
sudo sh TestScript.sh
Based on this file I create a test PKGBUILD to install TestScript.sh via pacman later on:
PKGBUILD
# Maintainer: Your Name <youremail#domain.com>
pkgname='test-script'
pkgver=1
pkgrel=1
pkgdesc="AUR test package"
arch=('x86_64')
license=('custom')
source=('TestScript.sh')
md5sums=('SKIP')
package() {
mkdir -p "${pkgdir}/usr/bin"
cp "${srcdir}/TestScript.sh" "${pkgdir}/usr/bin/TestScript"
chmod +x "${pkgdir}/usr/bin/TestScript"
}
Followed by
makepkg
sudo pacman -U test-script-1-1-x86_64.pkg.tar.xz
I can run
TestScript
from the command line. But just as I thought, I am unable to write into /usr/share/textfile.txt .
I was searching the Arch-Wiki page up and down, but I couldn't find out how to handle this situation. I basically just want to have a location where I can properly read and write a file without messing up the user space.
Does anyone have an idea?

How to source an external file in .bash_profile macOS

I tried to move my script from linux to mac, folder ~/Scripts placed in home with all other scripts,
but using source in .bash_profile won't call my script.
Contents of .bash_profile
f [ -f ~/.bashrc ]; then
. ~/.bashrc
. ~/Scripts/ipmitool
fi
alias test='echo test from bash_profile'
Contents of /Scripts/ipmitool
#!/bin/bash
if [[ -z $1 ]] ; then
printf "ipmitool <Host IP> \n\n"
else
ssh con_1 ipmitool -I lanplus -U * -P * -H $1 fru print
fi
Terminal
1232324fa:~ OiO$ ipmitool
-bash: ipmitool: command not found
1232324fa:~ OiO$ bash ~/Scripts/ipmitool
ipmitool <Host IP>
I need to use bash in front to be able to call ipmitool, in linux I just need to type ipmitool.
I tried to follow this How to source an external file in .bash_profile in OSX? but didn't work.
Could someone help this newb, Thanks.
It doesn't look like you want to source it. You want to add ~/Scripts to your path, so that later you can type ipmitool rather than ~/Scripts/ipmitool to execute it.
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
PATH=~/Scripts:$PATH
alias test='echo test from bash_profile'
(Make sure ~/Scripts/ipmitool is executable, as well, by running
chmod +x ~/Scripts/ipmitool
)

Assign output of mkdir command to variable

I am trying to assign the output of mkdir command to a variable. So I can use the directory further.
-bash-4.1$ pwd
/user/ravi/myscripts/tmpdata
-bash-4.1$ OUTPUT=$(mkdir tmpbkp.`date +%F`)
-bash-4.1$ ls | grep tmp
tmpbkp.2017-04-06
-bash-4.1$ echo "$OUTPUT"
But the directory name is not assigning to the variable. Could you please correct me where I am wrong.
When you run the mkdir command by itself, look how much output it produces:
$ mkdir foo
$
None!
When you use a command substitution to generate the argument to mkdir, look how much extra output you get:
$ mkdir tmpbkp.`date +%F`
$
None!
When you put it inside $() it still produces no output.
There is a -v option for mkdir (in the GNU version at least) which produces some output, but it's probably not what you want.
You want the name of the directory in a variable? Put it in a variable first, then call mkdir.
$ thedir=tmpbkp.`date +%F`
$ mkdir $thedir
$ echo $thedir
tmpbkp.2017-04-06
$

Bash: passing a variable to mv command option

--Bash 4.1.17 (running with Cygwin)
Hello, I am trying to pass the date into the --suffix option on the move (mv) command. I am able to pass in a simple string (like my name) but unable to pass in the date. If you run the script below you will see that the mv command with the suffix="$var" works but suffix="$now" does not.
#!/bin/bash
dir="your directory goes here"
now="$(date "+%m/%d/%y")"
var="_CARL!!!"
echo "$now"
echo "$var"
cd "$dir"
touch test.txt
# error if already exists
mkdir ./stack_question
touch ./stack_question/test.txt
mv -b --suffix="$var" test.txt ./stack_question/
The idea is that if test.txt already exists when trying to move the file, the file will have a suffix appended to it. So if you run this script with:
--suffix="$var"
you will see that the stack_question directory contains two files:
test.txt & test.txt_CARL!!!
But, if you run this script with:
--suffix="$now"
you will see that in the stack_question directory only contains:
test.txt
Any help on this would be greatly appreciated!
It is because you have embedded / in your date format try
now="$(date +%m_%d_%y)"

change terminal title according to the last 2 directories in the path

I want to change the title everytime I enter a new directory ( when using cd ), but show only the last 2 directories. I'm using tcsh at work and bash at home.
For example: if I'm at folder ~/work/stuff and I write: cd 1.1, I want my new title to be stuff/1.1.
I already know how to change the title everytime I change the folder:
alias cd 'cd \!*; echo "\033]0;`pwd`\a"'
And I know how to take only the 2 last directories:
pwd | awk -F / -v q="/" '{print $(NF-1)q$NF}'
The question is how to combine these two, or how to do it in a different way?
It doesn't have to be through alias to cd.
What I did was creating a script file named titleRename.tcsh with the following code:
#!/bin/tcsh -f
set fullpath = "`pwd`\a"
set newTitle = `echo $fullpath | awk -F / '{OFS="/"; if(NF > 2){printf $(NF-2);printf "/"; printf $(NF-1); printf "/"; print $NF;}else print $0}'`
echo "\033]0;$newTitle"
It splits the pwd with awk, getting only the last 3 directories, and then it prints to the tab name.
Then I added in the .alias file the following:
alias cd 'cd \!*; <dir of script file>/titleRename.tcsh'
Now the title name changes automatically whenever I cd to a different directory :)
I originally thought you should be able to use the full command where you have pwd in backticks in the alias ie:
alias cd 'cd \!*; echo "\033]0;`pwd | awk -F / -v q="/" '{print $(NF-1)q$NF}'`\a"'
but now I realise there may be problems with the nested quoting. And that wouldn't work in bash anyway; I don't think there's a way to access command parameters in an alias.
Instead of aliasing cd you should update the title with the prompt. I don't know tcsh, but in bash the normal way to do this sort of thing is with the special pseudo-variable PS1:
# Minimalist prompt
PS1="\$ "
# Additionally set terminal title to cwd
case "$TERM" in
xterm*|rxvt*)
PROMPT_DIRTRIM=2
PS1="\033]0;\w\a$PS1"
;;
*)
;;
esac
That won't trim the directory name quite the way you were doing it, but unfortunately I can't get the quoting right to be able to use the escape sequence in PROMPT_COMMAND.

Resources