You are not logged in.
This topic comes up everywhere all the time, but it does no harm to have one here too.
It's meant for short snippets of code that might be useful in a terminal or in a shell script. Please post your interesting discoveries or all-time favourites!
(Longer scripts that might trigger a discussion would be better off in their own thread.)
To start off:
Compare two directory trees
Meld is a handy GUI app for this job, but if you just want a quick check then diff can do directories too, I discovered while comparing icon themes:
diff -rq dir1 dir2
Of course, see 'man diff' for all kinds of options, and you can use grep to filter the results you're interested in:
diff -rq dir1 dir2 | grep 'Only in dir1'
etc...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline
A pretty basic copy tinkering (The idea is to get rid of file managers) with the "I want my prompt back at once" twist:
parallel
cp -r source1 dest &
cp -r source2 dest &
jobs # to list bg stuff
kill %1 # to kill job number 1
It gives some control and it is definitely better than clunky mc.
One could do a combo of serial and parallel as well:
(cp a b; cp d b) &
cp e b &
some good reading
http://mywiki.wooledge.org/ProcessManagement
Last edited by brontosaurusrex (2015-12-02 08:03:27)
Offline
One that I just learned earlier in the week, then led to another discovery:
Whenever I ran ntpdate i'd always want me to shut down the currently running ntp process. I'd give me a "port in use error". So when making any script, I'd have to make sure I shut ntp down, sleep for like 2 seconds, then re-run ntpdate. Then I'd have to make sure to restart my ntp process. Until I found that using
ntpdate -u
bypasses this need, it uses a randomized port so I can leave my normal ntp process up and running merrily!
These also led me to finding out that on debian, we have our own ntp command,
ntpdate-debian
It's pretty much the same command, but it uses our own ntp pools. Pretty neat, but the -u option was the most useful find.
"I have not failed, I have found 10,000 ways that will not work" -Edison
Offline
List all enabled units under systemd with:
systemctl list-unit-files|grep enabled
You can also use:
ls -lR /etc/systemd/system
ls -l /etc/systemd/system/multi-user.target.wants # View all user-enabled .services
These commands will show up any broken symlinks.
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
Bash <(process-substitution)comm
- to compare pkgs installed to all packages apt knows about
First lets count all package
apt-cache pkgnames | wc -l
Now comm is like diff but a little diff (heh heh no pun intended)
#comm -1 suppress lines unique to FILE1 column 1
#comm -2 suppress lines unique to FILE2 column 2
#comm -3 suppress lines unique to FILE3 column 3
not installed
comm -13 <(dpkg-query -l | grep '^i' | awk '{print $2}' | sed 's/\:.*//g' | sort ) <(apt-cache pkgnames | sort ) | wc -l
installed
comm -2 <(dpkg-query -l | grep '^i' | awk '{print $2}' | sed 's/\:.*//g' | sort ) <(apt-cache pkgnames | sort ) | wc -l
You could replace "wc -l " of course with grep | less / whatevuh
The numbers look pretty and add up on my end but not sure how accurate
aptitude of course does the same
aptitude -F "%p" search "?not(?installed)" | grep -v ':' | wc -l
but seems to list quite a bit more even after greping out other archs
Offline
Free up unused files for larger contiguous free space:
<redacted>
You will be amazed how much of yours and your family's hard drives space you can free up.
Help the IT guy at work and run it on the web server also.
Thanks all and I hope you have a wonderful day.
Mod Edit: Command redacted -HoaS
The meaning of life is to just be alive. It is so plain and so obvious
and so simple. And yet everybody rushes aroound in a great panic
as if it were necessary to achieve something beyond themselves.
- Alan Watts
Offline
Free up unused files for larger contiguous free space:
<redacted>
You will be amazed how much of yours and your family's hard drives space you can free up.
Help the IT guy at work and run it on the web server also.
Thanks all and I hope you have a wonderful day.
I don't know about that...
Offline
Free up unused files for larger contiguous free space:
<redacted>
You will be amazed how much of yours and your family's hard drives space you can free up.
Help the IT guy at work and run it on the web server also.
Thanks all and I hope you have a wonderful day.
"I have not failed, I have found 10,000 ways that will not work" -Edison
Offline
Free up unused files for larger contiguous free space:
<redacted>
Not funny
Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt «» BunsenLabs on DeviantArt
Offline
Free up unused files for larger contiguous free space:
<redacted>
This should probably be deleted before somebody actually tries it. Even though it is a public forum, this isn't 4chan, people should feel safe to come here for advice.
Offline
This should probably be deleted before somebody actually tries it. Even though it is a public forum, this isn't 4chan, people should feel safe to come here for advice.
Agreed.
@Temetka -- very funny, don't do that again
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
^+1, sorry Temetka, I can have a sick sense of humor too, but you can see the popularity of that command is not great.
Okay,
xwininfo comes installed with x11-utils
to awk out your screen width and height:
xwininfo -root | awk '/Width/ || /Height/{ print $2 }'
I export it in .xinitrc like so: (if put in bashrc it'll complain obviously without X session)
export WIDTH=$(xwininfo -root | awk '/Width/{ print $2 }')
export HEIGHT=$(xwininfo -root | awk '/Height/{ print $2 }')
now you can launch program like so
x-terminal-emulator -g 73x40+$(( $WIDTH / 3 ))+$(( $HEIGHT / 4 )) -e alsamixer
note: the + signs are part of geometry string
Last edited by gako (2015-12-03 07:35:04)
Offline
I like that one. Thanks, gako.
Offline
Ok, ok. I thought a bit of *NIX fun would be, well, fun.
Here's something useful. A guide on how to use tar to backup your home directory as taken from here:
https://mylinuxramblings.wordpress.com/ … directory/
In terminal type:
tar cf <name of archive>.tar.gz /home/<username>
In addition, when doing a tar backup, it’s also good to add the following flags : p & (z/j)
-p will preserve the original file permissions
-z will compress using gzip (medium cpu usage, but less space)
-j will compress using bzip2 (lots of cpu, even less space)
-v verbose output (optional)
So, a full backup would look something like this:
tar c(z/j)vf <name of archive>.tar.gz /home/<username>
In place of the .gz file name extension you could also use bz2.
For more info on tar command parameters, from Terminal enter man tar.
To Extract (restore the backup)
In terminal type:
tar x(z/j)vf <name of archive>.tar.gz
In place of the .gz file name extension you could also use bz2.
A Real Example
To backup my home directory to a tar file located in the root of a second hard disk I did the following:
a. Find out what disks and partitions exist:
sudo fdisk -l
This will display something similar to the screen-shot below
Change to the media directory
cd /media
b. Make a directory called disk1
sudo mkdir disk1
c. Mount my second hard disk (SDB1) to the disk1 directory
sudo mount /dev/sdb1 /media/disk
d. Change to the mounted disk
cd disk1
e. Enter the following to backup the home directory called myhomedirectory to a TAR file called mybackup.tar.gz:
tar czvf mybackup.tar.gz /home/myhomedirectory
this creates a file called mybackup.tar.gz in the /media/disk1 directory which is a backup of the home directory called myhomedirectory.
To restore, all I did under Crunchbang was to mount the second disk (as above) and from my Home directory enter:
tar xzvf mybackup.tar.gz
This restores the contents of the tar file into the root of my home directory. It includes the folder structure from root, but that is fine as it then allows me to copy over what I need for my new distro. I can then delete what is not needed. Furthermore, file permissions are maintained.
For more information on the tar file format go to http://en.wikipedia.org/wiki/Tar_%28file_format%29
The meaning of life is to just be alive. It is so plain and so obvious
and so simple. And yet everybody rushes aroound in a great panic
as if it were necessary to achieve something beyond themselves.
- Alan Watts
Offline
Ok, ok. I thought a bit of *NIX fun would be, well, fun.
There's fun, then there encouraging forum members to run a command that will completely erase their filesystem under the guise of "free up unused files". I don't think this is the kind of "fun" we want to encourage here, especially considering some of our forum members and user base are "green" enough to not know what not to do.
Posting fork bombs is something else we discourage as "the wrong kind of fun", since depending on how severely the system locks up and how dusty the user's heat sink is, they can send the computer into thermal shutdown.
End mod rant. We now return you to Handy command-line stuff for terminals and scripts. Have a nice day.
Be excellent to each other, and...party on, dudes!
BunsenLabs Forum Rules
Tending and defending the Flame since 2009
Offline
Posting fork bombs is something else we discourage as "the wrong kind of fun", since depending on how severely the system locks up and how dusty the user's heat sink is, they can send the computer into thermal shutdown.
Well said pv -- that was a severe lapse of judgement on my part and I am very sorry about it.
“Et ignotas animum dimittit in artes.” — Ovid, Metamorphoses, VIII., 18.
Offline
You can all blame it on "happy December" and move on
Offline
(There was a similar thread to this on Crunchbang btw, maybe some ideas there too.)
underline text
If you want to underline something in a script's output it's very easy using bash parameter substitution:
title='This is a section title'
echo "$title
${title//?/-}"
outputs:
This is a section title
-----------------------
You need a monospace font for it to line up properly. It looks good in a terminal.
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline
Bash now has "associative arrays" - this means that the keys can be strings as well as integers, and is very useful.
(You need to explicitly create one with 'declare -A arrayname'.)
eg
make list with unique elements
There are GNU tools for this, but it's easy enough just with bash now. Put the strings you want to organize in the keys of an array. If the same key already exists it will be overwritten, so you end up with unique items.
declare -A list
for i in 'first string' ' next string' ...
do
list[$i]=1
done
Now you can get your new cleaned-up list with
for i in "${!list[@]}"
do
echo "$i" # or whatever
done
(In some cases there might be some useful information you can put in the array element instead of "1".)
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline
error checking in script
Found today - automatic checking for any command returning non-zero. Turn it on for a certain section like this:
trap 'echo "ERROR!
line: $BASH_LINENO
cmd: $BASH_COMMAND"' ERR
ekk # non-existent command
# do other stuff...
# turn off again
trap - ERR
Outputs:
john@bunsen:~$ ekk
bash: ekk: command not found
ERROR!
line:
cmd: ekk
Probably $BASH_LINENO only works inside a script, not a terminal. Of course you might well want to do something more useful with those two BASH_ variables than just echo them...
...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )
Offline