Configuration files management - configuration-files

I have a bunch of configuration files used by gradle.
conf1 conf2 conf3 conf4 etc.
Sometimes I need to change some setting in every file or in several files.
Is there any tool(for Linux) that helps me to do that?
For example, if I want set settings1 in conf2, conf3 and in conf5 I would use something like
"set settings1=value in conf2,conf3,conf5"

You could do it with sed. Needing to change some but not all of a set of similarly-named files in a folder complicates it a bit:
find . -name conf2 -o -name conf3 -o -name conf5 | xargs sed -i 's/settings1=/settings1=value/g'
If you were changing all of the conf files you could just do this:
sed -i 's/settings1=/settings1=value/g' conf*

Related

how to print the progress of the files being copied in bash [duplicate]

I suppose I could compare the number of files in the source directory to the number of files in the target directory as cp progresses, or perhaps do it with folder size instead? I tried to find examples, but all bash progress bars seem to be written for copying single files. I want to copy a bunch of files (or a directory, if the former is not possible).
You can also use rsync instead of cp like this:
rsync -Pa source destination
Which will give you a progress bar and estimated time of completion. Very handy.
To show a progress bar while doing a recursive copy of files & folders & subfolders (including links and file attributes), you can use gcp (easily installed in Ubuntu and Debian by running "sudo apt-get install gcp"):
gcp -rf SRC DEST
Here is the typical output while copying a large folder of files:
Copying 1.33 GiB 73% |##################### | 230.19 M/s ETA: 00:00:07
Notice that it shows just one progress bar for the whole operation, whereas if you want a single progress bar per file, you can use rsync:
rsync -ah --progress SRC DEST
You may have a look at the tool vcp. Thats a simple copy tool with two progress bars: One for the current file, and one for overall.
EDIT
Here is the link to the sources: http://members.iinet.net.au/~lynx/vcp/
Manpage can be found here: http://linux.die.net/man/1/vcp
Most distributions have a package for it.
Here another solution: Use the tool bar
You could invoke it like this:
#!/bin/bash
filesize=$(du -sb ${1} | awk '{ print $1 }')
tar -cf - -C ${1} ./ | bar --size ${filesize} | tar -xf - -C ${2}
You have to go the way over tar, and it will be inaccurate on small files. Also you must take care that the target directory exists. But it is a way.
My preferred option is Advanced Copy, as it uses the original cp source files.
$ wget http://ftp.gnu.org/gnu/coreutils/coreutils-8.21.tar.xz
$ tar xvJf coreutils-8.21.tar.xz
$ cd coreutils-8.21/
$ wget --no-check-certificate wget https://raw.githubusercontent.com/jarun/advcpmv/master/advcpmv-0.8-8.32.patch
$ patch -p1 -i advcpmv-0.8-8.32.patch
$ ./configure
$ make
The new programs are now located in src/cp and src/mv. You may choose to replace your existing commands:
$ sudo cp src/cp /usr/local/bin/cp
$ sudo cp src/mv /usr/local/bin/mv
Then you can use cp as usual, or specify -g to show the progress bar:
$ cp -g src dest
A simple unix way is to go to the destination directory and do watch -n 5 du -s . Perhaps make it more pretty by showing as a bar . This can help in environments where you have just the standard unix utils and no scope of installing additional files . du-sh is the key , watch is to just do every 5 seconds.
Pros : Works on any unix system Cons : No Progress Bar
To add another option, you can use cpv. It uses pv to imitate the usage of cp.
It works like pv but you can use it to recursively copy directories
You can get it here
There's a tool pv to do this exact thing: http://www.ivarch.com/programs/pv.shtml
There's a ubuntu version in apt
How about something like
find . -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /DEST/$(dirname {})
It finds all the files in the current directory, pipes that through PV while giving PV an estimated size so the progress meter works and then piping that to a CP command with the --parents flag so the DEST path matches the SRC path.
One problem I have yet to overcome is that if you issue this command
find /home/user/test -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /www/test/$(dirname {})
the destination path becomes /www/test/home/user/test/....FILES... and I am unsure how to tell the command to get rid of the '/home/user/test' part. That why I have to run it from inside the SRC directory.
Check the source code for progress_bar in the below git repository of mine
https://github.com/Kiran-Bose/supreme
Also try custom bash script package supreme to verify how progress bar work with cp and mv comands
Functionality overview
(1)Open Apps
----Firefox
----Calculator
----Settings
(2)Manage Files
----Search
----Navigate
----Quick access
|----Select File(s)
|----Inverse Selection
|----Make directory
|----Make file
|----Open
|----Copy
|----Move
|----Delete
|----Rename
|----Send to Device
|----Properties
(3)Manage Phone
----Move/Copy from phone
----Move/Copy to phone
----Sync folders
(4)Manage USB
----Move/Copy from USB
----Move/Copy to USB
There is command progress, https://github.com/Xfennec/progress, coreutils progress viewer.
Just run progress in another terminal to see the copy/move progress. For continuous monitoring use -M flag.

how to set folder path for gtags

I am new to gtags, and have a question. I have a big project, such as android AOSP, I want gtags to parse some folders, how can I achieve it with gtags? I searched and got solution:
use -f option with gtags, it seems doesn't support folders
Is there any good idea that I can set the folders path and gtags only process those folders?
UPDATE: author of the question came up with a better solution in the comments. I'm adding it here so it's easier to find:
.. create tag file in the sub-directories I need, and add the directories
to GTAGSLIBPATH when loading the project,
My answer:
You can limit what gtags indexes by adding list of files/directories to skip keyword in ~/.globalrc or /etc/gtags.conf. Here's a sample gtags.conf file.
The problem is that often global/gtags packages don't install gtags.conf (at least it's not there in global-5.7.1-2 on ubuntu 12.04), so you'll need to either get it from global source distribution, or use someone else's gtags.conf as a reference. For instance here.
Something like this should work. Note that leading / means from the top of the tree. Without it gtags will skip matching entries anywhere in the tree.:
common:\
:skip=/skip-this-dir/,/lib/and-this/,/include/and-this-one-too/:
The -f option is premised on find(1). Please try the followings.
$ find folder1 folder2 folder3 -type f -print | gtags -f -
or
$ find folder1 folder2 folder3 -type f -print >gtags.files
$ gtags
This is my bash function to get rid of files and paths including 'dummy' and 'win':
function gtagsupdate {
find . -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" | grep -v dummy | grep -v win | gtags -f -
}

Need to find list of scripts that uses a certain file

I have a file named "performance". I need to know which scripts use this file.
I don't believe there is a straight forward way of listing files used by scripts. You will have to run grep in combination of find to check if the script contains the name of the file that you want to check for. Knowing the exact name of the file will help. Using words like performance might end up grepping files that uses that word in comments.
find /path/ \( -name "*.sh" -o -name "*.pl" \) -type f -print0 | xargs -0 grep "performance"
If you're on linux, then you may install and configure auditd to watch for accesses to a particular file.
You can use the -r option to recursively grep through all sub-directories and find text. The syntax is as follows:
grep -r "performance" /dir/

Remove string/script from all files (recursive)

One of my websites has been hacked, all the index.html and index.php files have been infected with a certain Javascript. I would like to have a unix command to remove this script from all files.
Script is here: http://pastie.org/private/6osrvd5zhphe372gblrc6w
I am trying to figure this out with sed but no luck so far
Thanks!
sed -i 's/<script>.*<\/script>//' fileName
will remove the tag script and all its content.
This works if you only have one <script> tag.
If you haven't only one, extend it with try keyword in the following way
sed -i 's/<script>try.*<\/script>//' fileName
Edit
If you want to do it on all files in a recursive way, you can use a find command like this:
find . -name "index.html" -print | xargs sed -i 's/<script>try.*<\/script>//' fileName
where . is the current directory
You can try this
find src/ -name "index.html" -print | xargs sed -i 's/<script>try{document.body++}catch(dgsgsdg){zxc=12;ww=window;}if(zxc).*<\/scri‌​pt>//
perl -pi -e 's/<script>.*<\/script>//g' index.html

Create symbolic link from find

I'm trying to create a symbolic link (soft link) from the results of a find command. I'm using sed to remove the ./ that precedes the file name. I'm doing this so I can paste the file name to the end of the path where the link will be saved. I'm working on this with Ubuntu Server 8.04.
I learned from this post, which is kind of the solution to my problem but not quite-
How do I selectively create symbolic links to specific files in another directory in LINUX?
The resulting file name didn't work, though, so I started trying to learn awk and then decided on sed.
I'm using a one-line loop to accomplish this. The problem is that the structure of the loop is separating the filename, creating a link for each word in the filename. There are quite a few files and I would like to automate the process with each link taking the filename of the file it's linked to.
I'm comfortable with basic bash commands but I'm far from being a command line expert. I started this with ls and awk and moved to find and sed. My sed syntax could probably be better but I've learned this in two days and I'm kind of stuck now.
for t in find -type f -name "*txt*" | sed -e 's/.//' -e 's$/$$'; do echo ln -s $t ../folder2/$t; done
Any help or tips would be greatly appreciated. Thanks.
Easier:
Go to the folder where you want to have the files in and do:
find /path/with/files -type f -name "*txt*" -exec ln -s {} . ';'
Execute your for loop like this:
(IFS=$'\n'; for t in `find -type f -name "*txt*" | sed 's|.*/||'`; do ln -s $t ../folder2/$t; done)
By setting the IFS to only a newline, you should be able to read the entire filename without getting splitted at space.
The brackets are to make sure the loop is executed in a sub-shell and the IFS of the current shell does not get changed.