Change folder permissions and ownership from command line

This post shows how to change permission and ownership from command line on Linux OS.

Use chown to change ownership and chmod to change permission.

The -R option makes them change the permissions and ownership for all files and directories inside of the given directory.

The following command will change ownership (both user and group) of all files and directories inside your_directory and including your_directory itself:

$ sudo chown -R username:group your_directory

And the following command will only change the permission of the folder directory but will leave the ownership of the files and folders inside the directory not changed:

$ sudo chown username:group your_directory

Note that you need to use sudo to change the ownership from root to yourself.

To make the current user own everything inside the folder (and the folder itself), use the following command:

$ sudo chown -R $USER your_directory

$ sudo chown -R $USER:$USER your_directory

For more commonly used Linux commands, check my other posts at here  and here .

Download files using Shell script

This post provides instructions on how to download files using Shell script.

To download a number of files that have similar file path, see an example as follows:

http://example.com/path/to/file1.csv
http://example.com/path/to/file2.csv
http://example.com/path/to/file3.csv
http://example.com/path/to/file4.csv
.
.
http://example.com/path/to/file4999.csv
http://example.com/path/to//file5000.csv

Method 1: 

$ wget http://example.com/path/to/file{1..5000}.csv

Method 2:

write a shell script as the following:

for i in {1..5000}
do
wget http://example.com/path/to/file$i.csv;
done

 

For more commonly used Linux commands, check my other posts at here  and here .

Find IP address on Ubuntu

This post introduces how to find the IP address on your Ubuntu.

Open your terminal (CTRL + ALT + T), and then issue the following command.

$ ifconfig | less

your IP should be listed under

inet addr

Something looks like this:

inet addr:10.0.5.25 Bcast: 10.0.5.255 Mask:255.255.255.0

In this example, 10.0.5.25 would be your IP address.

Note: press “q” to exit.

 

Shell Scripting Tutorials (Pic-notes)

This post provides some pic notes for Shell Scripting Tutorials (a list of 62 short videos) .

No capital letters in bash shell command (All should be in lowercase)!

clear

who

pwd 

cal

date

touch — create empty (plain text) files

mkdir — create directory (directory is the same thing as folder, in terminal it is called directory, on GUI interface it is called folder)

cd  — change directory

cat

cat > test   — this will create a file called test and write text into the file. (Ctrl+ D to exit editing the file.)

cat < test (shortcut cat test  —  this will open an existing file called test and print its content in the terminal.

cat random test > sample  — this will merge two existing files called random and test, and write the content to a new file called sample

mv — rename (when the source and destination is in the same directory) or move files

rm

rm -r  (r means recursive)

rmdir   — rmdir (only for empty directory)

cp  — copy files and /or directories

ln file1 file 2 — (file 1 is a physical copy of file 2, when file 1 changes, file 2 will be updated automatically and in real time)

ln -s file1 file1_soft  — (file1_soft is a soft link  of file1, when file1 was removed, file1_soft will not work, because the physical copy it refers to has been gone.)

 

 

 

file * command

the out put of wc command:

number of line, word, and charactor in the input file.

wc -l           number of lines

wc -w         number of words

wc -c          number of characters

 

 

  • Shell Scripting Tutorial-12: Sort

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-f   file

-d   direcotry

-s file size (greater than zero or not)

-c (character file, e.g.,  .dbf file)

-b (block file like video and image)

-r  (read permission)

-w (write permission)

-x   (executable or not)

cat >> $fname   — will not overwrite the existing content in the file and will append … cat > $fname will overwrite

-n (the length of the string is not zero)

-z (the length of the string is zero)

the output of $?  is 0 means the operation is true, if false, the output is 1.

 

-a  AND logic operator

because there is a new line symbol, so need to be -eq 2 for a character.

 

 

 

-0  or logic operator

|    pipe symbol

the output of echo $var   as the input of wc -c, use reverse quote symbol to group it as a whole for -eq  operator

|  — the pipe symbol tells the case not to end the case statement after checking [aeiou]*, need to check [AEIOU]* as well for the first case statement.

Normally 0 is for success, 1 is for failure of the result of $?

2 means invalid.

the default IFS is white space.

exec and tty command

exec — exec is a builtin command of the Bash shell. It allows you to execute a command that completely replaces the current process. The current shellprocess is destroyed, and entirely replaced by the command you specify. (more check here.)

tty — Print the file name of the terminal connected to standard input. ( check more at here.)

read — read is a builtin command of the Bash shell, which reads a line of text from standard input and splits it into words. These words can then be used as the input for other commands. (for more info check here.)

sleep command — used for delay (the unit of its parameter is in seconds)

(see also Shell Scripting Tutorial-11: Count Lines, Words & Characters Using ‘wc’)

In this tutorial you’ll learn to iterate over contents of a text file using for loop.

 

In this tutorial you’ll learn to use nested loops to print all possible combinations of digits 1, 2 and 3.

 

In this tutorial you’ll learn to use the break statement to transfer control from within the loop to the first statement after the loop.

In this tutorial you’ll learn to use the ‘continue’ statement to skip iterations in loops.

 

In this tutorial you’ll learn to execute multiple commands on a single line in the terminal without using a shell script.

In this tutorial you’ll learn to add or remove users on the system, also we’ll check out the ‘login’ command to login through a user account in a terminal window.

In this tutorial we’ll build a script that checks for a user every minute and then notifies us when it logs in and also displays the time the user was late in logging in.

In this tutorial we’ll improve the script we built in the previous lesson.

In this tutorial we’ll finish our work on the script we first met in the 56th tutorial. We’ll check every one second for the user and will also report time with greater precision.

In this tutorial you’ll learn to send messages to other users on your system using the ‘write’ command.

You’ll also learn to use ‘finger’ command to see which users who have disabled message reception and which have not.

one terminal (terminal 1):

Another terminal (terminal 2):

in terminal 1:

in Terminal 2:

In Terminal 1:

In terminal 2:

In terminal 1:

In Terminal 2:

EOF — End Of File

 

Finger command:

In terminal 1:

In Terminal 2:

 

In Terminal 1:

Note the * before pts/1 (that indicates newuser2 does not allow send msg to it)

 

In this tutorial you’ll learn to create your own commands using functions.

unset command to remove customized functions in terminal.

 

In this tutorial we’ll learn to execute a script from another script; not a particularly hard thing to do but I recorded a tutorial on it anyway.

Thank you so much for watching the tutorials in this course. I hope you guys had as much fun watching the tutorials as I had recording them for you.

The END:) happy shell scripting!

 

 

 

 

 

 

 

Bash shell scripting resources

This post provides some useful (video) resources for bash shell scripting. (If you do not like video style tutorials, check here for a post that is not video based.)

The bash ( Bourne Again Shell) is the most common shell installed with Linux distributions and Mac OS.

  • Bash Basics (A video playlist with 9 videos) — The Basics of using Bash in Linux with a focus on Ubuntu.

Bash Basics Part 1 of 8 | Access and Navigation

       $ means you logged in as a normal user.

       # means logged in as a root user.

Bash Basics Part 3 of 8 | Privileges and Permissions

Bash Basics Part 8 of 8 | Bash Scripting