How to rename files in a directory using bash script [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I wanted to find a bash script for renaming files with _ (underscore) to - (hyphen)
for example changing file name my_page_name.php to my-page-name.php,
keeping the name of the file of those without .php extension same
I attempted:
Nothing yet , just used the script found here bbs.archlinux.org/viewtopic.php?id=36305 and replace space with _ and underscore with -

If you only need to do it in one directory (and not subdirectories):
for f in *_*; do mv "$f" "${f//_/-}"; done
Otherwise, you can use find to -exec a bash subshell.

Use the rename program:
rename s:_:-: *.php

Related

Bash replacing content INLINE using a WGET and CAT commands [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am working on some kind of version control for a specific software (Bash script.)
When a new version releases, the code should be updated to the latest release, I have figured this out but I seem stuck and can't make the replacement inline.
#First we download the source code with wget, it returns a text with the new code
wget www.example.com/sourcecode | cat . > $0
How can I redirect that output (text / script) to the current script who's executing it and replace it.
Note $0 gives us the location of the current script. So the cat command, just should replace the new text coming from wget to the current script.
Thanks!
Your question is a bit confused, so I can try to guess your needs.
Are you asking how to capture the output of the commmand you posted?
If yes, the solution is:
source_file_content=$(wget -O - http://www.example.com/sourcecode)
# Do anything with ${source_file_content}
Let me know if it is right for you.

copy file from directory in directory stack to current directory [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I often find myself wanting to copy files from one of the directories in my directory stack to another, and the best solution I've come up with is cp $(dirs -p | tail -n 1)/somefile.txt ./somefile.txt
Is there a better solution for this?
You can access the directory stack directly via the DIRSTACK array; no command substitutions are needed.
cp "${DIRSTACK[-1]}/somefile.txt" .
Your original code was buggy for the same reason the output of ls should not be used programmatically; a directory name containing a newline character could break it. For example:
$ pushd $'foo\nbar'
$ dirs -p | head -1
~/bin/foo
Using dirs +0 at least fixes that problem (assuming you correctly quote the command substitution as cp "$(dirs +N)"/somefile.txt ./somefile.txt, anyway):
$ dirs +0
~/bin/foo
bar
After referncing the https://www.gnu.org/software/bash/manual/html_node/Directory-Stack-Builtins.html#Directory-Stack-Builtins, a slightly better solution is cp $(dirs +N)/somefile.txt ./somefile.txt

Removing files that have special characters in their name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
How do I remove files, with specific names.. For example, my file name is
This File Example (England).txt
This File Example (US).txt
I want to remove all the files with (England) in the name.
So I tried,
rm -f "*(Eng*"
But it doesn't work. Is there something needed for special chars?
This should do it:
rm -f ./*'(England)'*
Escape the special ones:
rm -f *\(Eng*
^
Or use single quotes for literal parts:
rm -f *'(Eng'*
^ ^

How can I create .sh extension file in Linux Ubuntu? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a script to run one of my .jar files as daemons, but I am not understanding how to create a .sh extension file in Ubuntu. I have already used vi to create a file with the code that I want, but I cannot figure out how to define the file to specifically be a .sh file. For example, I want to convert my file "foo" or "foo.txt" into "foo.sh".
Is there a simple command I am not seeing that can convert files to a .sh extension or is it a more complicated process?
Use this as a template:
#!/bin/bash
# content of your script
The first line is called a shebang and tells the OS what program that should be used executing the content of the file, in this case, bash.
To make the file executable:
chmod 755 foo.sh
To execute your script do:
./foo.sh

Changing the current working directory of invoking bash programmatically [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file which contains key/value -pairs in the format
TAG PATH
. Now, I want to write a program that reads from the command line a TAG, and changes the current working directory of invoking bash to corresponding PATH.
How would I go about this?
You might consider something like (perhaps in your bash function)
while read tagname dirname ; do
pushd $dirname ;
dosomethingwith $tagname
popd
done < yourinputfile.txt
See this question (about read in bash) and read the advanced bash scripting guide
GNU awk might be a better tool.
Notice that you can only change the current directory of the current process using the chdir(2) syscall (invoked by cd or pushd or popd bash builtins). There is no way to change the directory of some other process (e.g. the parent or invoking one). The pushd and popd builtins also updates bash directory stack.
There is no way to change the current directory of the invoking shell (the parent process running in a terminal). But you may define your own bash function there. Read Advanced Linux Programming to understand more about Unix processes

Resources