DEV Community

Arun Kumar Singh
Arun Kumar Singh

Posted on

1 1

Shell Tips

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site