Showing posts with label linux_commands. Show all posts
Showing posts with label linux_commands. Show all posts

Friday, November 11, 2011

Linux show total lines of files in a directory (sorted by line numbers)

Linux showing total number of lines of each file in a directory:

wc -l *

Even better, you can have the results sorted by line numbers:

wc -l * | sort -n -k1

-n         compare according to string numerical value
-k#       define start position

Thursday, October 20, 2011

Linux add user

adduser username
not useradd username

Story: 

######## Stage 1. Stuck & Confused ############

I'm was trying to set up my Rackspace server and I ran into some questions during my created user account.

From root, I added a user with "useradd alice". When I discovered that ~/ failed, I had to sudo mkdir /home/alice.

Q0: It's so strange that the user home dir has to be manually created

Then I logged in as alice, but found that the prompt is not customized and lacks color. Since useradd copied /etc/skel/.bashrc from root to alice, I thought that I can just "cp /etc/skel/.bashrc ~/.bashrc", but when I did that (with sudo), "echo $PS1" still shows the old original prompt settings.

Q1: How come the ~/.bashrc, identical in root and user alice, does not work in alice?

I even uncommented out "force_color_prompt=yes", but its still not working.

Q2: How come user alice needs sudo to edit ~/.bashrc in the first place when its permissions are (from ls -l):
        alice@simba:~$ ls -l ~/.bashrc
        -rw-r--r-- 1 root root 141 2011-10-20 05:19 /home/alice/.bashrc

Answer: Because the user and group of /home/alice/.bashrc are both set to "root" (The user is the one that matters here)

Then I tried to "sudo chown alice:alice ~/.bashrc", but was denied permission because I was on alice.


######### Stage 2. Found good resource ############

Then I read this informative article and learned:

useradd -D shows the defaults of useradd. You can change the shell to bash by specifying useradd -D -s /bin/bash . Then you can use useradd alice followed by passwd alice to set its password.  BUT!! This still doesn't automatically make /home/alice for you


######## Stage 3. Problems solved! ################

adduser alice worked beautifully
0: home dir created automatically :D
1: bashrc and xterm-color both work :D
2: home dir's files are automatically made with alice as the user
# adduser alice
Adding user `alice' ...
Adding new group `alice' (1001) ...
Adding new user `alice' (1001) with group `alice' ...
Creating home directory `/home/alice' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for alice
Enter the new value, or press ENTER for the default
        Full Name []: Alice P Hacker
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
Is the information correct? [Y/n] y
... Why does it care about the room number? haha

Thursday, October 13, 2011

Setting up rackspace server

First I would like to say: thank you Rackspace, you have given me back the fire spark in life that has been swallowed by the fire hose.  Your superb user experience with instant server setup and live technical chat support has been a very pleasant surprise.  I can get all that for just $0.015/hour ($10.8/month)?

Anyways ... the things I did/learned from setting up my first Rackspace server:

1. Setting up apache for Python. I do want to run some Django apps, so I followed steps 1 and 2 of a previous setup.

2. DNS ... When I set up a domain name inside rackspace, it's going to be an internal lookup. I'll give my registered domain on name.com the general rackspace name. Then rackspace is going to do an internal lookup to find my specific machine that hosts the domain name.

3. History is found in ~/.bash_history


I created a user from root. How come the user only has sh shell but not the better bash shell like root?
Edit: I think there is more to it: useradd -D --shell <shell-to-set>

4. Check the shell with ps or echo $SHELL

5. Check the shell a particular user is running in /etc/passwd, or view it like grep username /etc/passwd

6. So I changed my user's entry in /etc/passwd from /bin/sh to /bin/bash

7. Move /bin/bash to the top of /etc/shells


Now my user has bash, but it doesn't have .bashrc. There is more to it.

8. Copy root's /etc/skel directory. "The /etc/skel directory contains files and directories that are automatically copied over to a new user's home directory when such user is created by the useradd program." ~ source 

It turns out that I do have them copied, but I have to manually copy them again into ~/, which I'll have to create first, but I need sudo powers for my user, see next post.

Friday, August 5, 2011

scp command

copy from current location to another machine which has an IP

scp [-r] source destination
scp -r path/to/src-dir/ username@machine-to-go-to-IP:/path/to/dest-dir

Wednesday, June 22, 2011

grep skip directories

grep -ir "searching me" . | grep -v "dir_name_to_skip"

Sunday, June 12, 2011

Buffer Overflow w/ Perl/Python multiply the same character, repeat character

$ ./program `python -c "print 'a'*300 + 'A'*20"`
Segmentation fault (core dumped)

$ ./program `perl -e "print 'a'x300 . 'A'x20"`
Segmentation fault (core dumped)

Thursday, June 9, 2011

xargs magic

saw someone do this:

(playdoh)haoqili@host-3-248:11:25:47:~/dev/playdoh/playdoh/playdoh$ git ls-files | grep pyc | xargs git rm
rm '__init__.pyc'
rm 'apps/commons/__init__.pyc'
rm 'apps/commons/context_processors.pyc'
rm 'apps/commons/helpers.pyc'
rm 'apps/commons/middleware.pyc'
rm 'apps/commons/models.pyc'
rm 'apps/commons/urlresolvers.pyc'
rm 'apps/examples/__init__.pyc'
rm 'apps/examples/models.pyc'
rm 'apps/examples/urls.pyc'
rm 'apps/examples/views.pyc'
rm 'msw/__init__.pyc'
rm 'msw/models.pyc'
rm 'msw/urls.pyc'
rm 'msw/views.pyc'
rm 'settings.pyc'
rm 'urls.pyc'

Saturday, April 16, 2011

Looking for things in Linux

Search for part of a filename:
find . -name "*partOfFileName*"
Search for part of content:
grep -ir stringToSearch .
Search for previous command:
history | grep partOfCommand

Friday, April 15, 2011

merge pdf's on linux

Use the command PDFTK!!
pdftk *.pdf cat output result.pdf
pdftk first.pdf second.pdf third.pdf cat output result.pdf

it does splits too!

Sunday, February 13, 2011

today I learned

Ubuntu: To switch windows from one desktop to the other (accidental discovery):
- SHIFT+CTRL+ALT+Arrow Right or Left

Linux, from 6.033 hands on 1:
Jotting down notes as I go along ... feel free to point out mistakes
- stat [file_or_dir]
- The difference between symbolic link and hard link (Phil introduced this concept to me 2 years ago, but now I finally get it). I've always used symbolic link and it's the way to go!
- echo $PATH "tells the shell where to look in the file system for programs we type on the command line"
- you can do "setenv OLDPATH $PATH" in "tcsh -f", not in bash shell
- echo $0 or ps -p $$ finds what shell I'm in.