

#BASH SCRIPT GET FILE PATH HOW TO#
How to get script name, script path within the bash script in LinuxĮcho $script_name2 deepak]#.


#endif Bash - get current script directory path Printf("\nfull path = %s, directory=%s program=%s\n",buffer,dirname(pgm),basename(pgm)) #if 0 // set to 1 to test as stand-alone, leave as 0 if you want to include this function in your code. If ((len = readlink ("/proc/self/exe", buffer, sizeof (buffer) - 1)) != -1)įprintf (stderr, "\n%s version 0.9, date Compiled: %s, ", cp+1, _DATE_) įprintf (stderr, "is executed from %s\n", buffer) //program executes, dirname ok Sometimes you want to know the program source A c function to get your own executable's execution directory. In this tutorial you experimented with the for loop in your shell and used it to iterate over files and apply commands.Previous Post Next Post How to Get Bash Script’s Own Path The backups are created in the new location. for FILE in * do cp $FILE "/tmp/my-backups/ $FILE.bak" done.The following command stores the backups in the folder /tmp/my-backups, provided the directory already exists: You’re not limited to making copies in the same directory either you can specify a new path for your backup files. You now have exact copies of each of your files, with an extension that indicates that they are backup files. Now each file contains the name of the file on the first line, and a universal truth about loops on the second line. Now iterate through each file and print its contents: The exclamation point needs to be escaped with a backslash so the shell doesn’t interpret the character as a shell command. The -e flag on echo ensures that newlines are preserved. for FILE in * do echo -e " $FILE \nLoops Rule\!" > $FILE done.The files you created in the previous section were all created with the touch command and are empty, so showing out how to cat each file wouldn’t be very useful.įortunately, using a similar for loop, you can insert text into each file with a single command.Įxecute the following command to insert the file’s name, followed by a newline, followed by the text Loops Rule! into each file: Now that you know how to loop through the files in a directory and spit out the names, let’s use those files with a command. You could change the wild card could to file-* to target all of the files that started with file-, or to *.txt to grab just the text files. That tells the for loop to grab every single file in the directory. You probably noticed we’re using the wild card character, *, in there. To loop through a directory, and then print the name of the file, execute the following command:

You can also create these files quickly using brace expansion and a range: Now use the touch command to create a few text files: You can apply the following commands to any directory of your choosing, but for this tutorial, create a directory and some files to play around with. This particular control flow method is baked into the Bash or zsh command-line shell. You are going to use the for shell scripting loop in this tutorial. In this tutorial you’ll iterate over files and apply commands using either the Bash or zsh shells. It allows you to apply the same logic over and over to a group of items with minimal code. Looping is one of the most powerful things you can do in programming. Not all command-line utilities allow you to run a command against multiple files, but with the power of shell scripting and the for loop, you can super charge any command you choose.
