If you want to print only some fields from file use "cut" command
e.g.
You want to list accounts on your system you can obtain it from /etc/passwd:
cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
We are interested only in the first field delimited with ":"
so we run:
cat /etc/passwd | cut -d":" -f1
the first cut parameter "-d" tells what field delimiter will be, the second "-f" says which field number should be printed
Sunday, June 28, 2009
BASH: bind directories
If you want to bind one directory to another run this command:
mount --bind /source/directory /newly/created/destination/directory
Note: you need to create /newly/created/destination/directory to be able to bind content of /source/directory to it.
To permanently have this binding edit your /etc/fstab and put this line:
/source/directory /newly/created/destination/directory none bind 0 0
Next run this command:
mount -a
mount --bind /source/directory /newly/created/destination/directory
Note: you need to create /newly/created/destination/directory to be able to bind content of /source/directory to it.
To permanently have this binding edit your /etc/fstab and put this line:
/source/directory /newly/created/destination/directory none bind 0 0
Next run this command:
mount -a
BASH: reload fstab
If you made some changes in /etc/fstab and want to reload it so they will become visible just run with root privelages:
mount -a
mount -a
Saturday, May 23, 2009
BASH: count files in directory and subdirectories
There is a simple way to count number of files and subdirectories
find /path/to/my/dir -type f | wc -l
find /path/to/my/dir -type f | wc -l
Wednesday, May 13, 2009
BASH and sed: revert file content
If you want to revert file content you can use sed:
cat myFile.txt | sed -n -e '1!G' -e 'h' -e '$p'
Explanation:
sed -n
surpress printing lines
-e '1!G'
append to the pattern bufor content of the temporary bufor (applies to all lines except the first one(
-e 'h'
overwrite temporary bufor with current pattern content
-e '$p'
end of file found - print pattern bufor
cat myFile.txt | sed -n -e '1!G' -e 'h' -e '$p'
Explanation:
sed -n
surpress printing lines
-e '1!G'
append to the pattern bufor content of the temporary bufor (applies to all lines except the first one(
-e 'h'
overwrite temporary bufor with current pattern content
-e '$p'
end of file found - print pattern bufor
Sunday, May 10, 2009
sed: replace string with another one in files
Here is the script to replace string in files to another one
phrase_in=$1
phrase_out=$2
if [ ! "$#" -eq "2" ]; then
echo "incorrect number of arguments"
exit
fi
echo $phrase_in
echo $phrase_out
for filename in `grep -r -l $phrase_in .`
do
sed -i "s:$phrase_in:$phrase_out:g" $filename
echo sed -i s:$phrase_in:$phrase_out:g $filename
done
You execute it with two parameters: string_to_search and string_to_replace
Of course there are also other solutions for this problem.
http://www.jonasblog.com/2006/05/search-and-replace-in-all-files-within-a-directory-recursively.html
But this script is mine :)
phrase_in=$1
phrase_out=$2
if [ ! "$#" -eq "2" ]; then
echo "incorrect number of arguments"
exit
fi
echo $phrase_in
echo $phrase_out
for filename in `grep -r -l $phrase_in .`
do
sed -i "s:$phrase_in:$phrase_out:g" $filename
echo sed -i s:$phrase_in:$phrase_out:g $filename
done
You execute it with two parameters: string_to_search and string_to_replace
Of course there are also other solutions for this problem.
http://www.jonasblog.com/2006/05/search-and-replace-in-all-files-within-a-directory-recursively.html
But this script is mine :)
Monday, April 27, 2009
Git: check what files you changed before pushing
To make sure what files you have changed before pushing them on remote repo run
git diff HEAD origin/master
If you are pushing to branch replace "master" with remote branch name
git diff HEAD origin/master
If you are pushing to branch replace "master" with remote branch name
Subscribe to:
Posts (Atom)