DEV Community

loading...

Shell Tips

Arun Kumar Singh
In quest of understanding How Systems Work !
・1 min read

Extract File name in bash:-

You can use basename to Strip directory and suffix from filenames

for i in /tmp/files/*/*; do
 key=$(basename $i)
Enter fullscreen mode Exit fullscreen mode

Add Date in File or folder Name:-

In case you have requirement of adding date in your file or folder name

now=$(date +"%m-%d-%Y")
printf "%s\n" $now
Enter fullscreen mode Exit fullscreen mode

User Input:-

read command can be used to take input from user.

read -p "Please input $inp? [yn]" answer
if [[ $answer = y ]] ; then
  # run the command
  echo "Yes"
fi

Enter fullscreen mode Exit fullscreen mode

Use set -x to watch execution

By default, after the script is executed, it only displays the results. You can use set -x to output the executed command line before its execution result.

root@bd92bbce2f7d:/# cat dummy.sh
#!/usr/bin/env bash
set -x
echo Step 1
echo Step 2


root@bd92bbce2f7d:/# sh dummy.sh
+ echo Step 1
Step 1
+ echo Step 2
Step 2
Enter fullscreen mode Exit fullscreen mode

I will keep updating this post, if I find something useful. Thanks

Discussion (0)