Skip to main content

Posts

Showing posts with the label Linux for noob

Linux bash script get folder size

Having an error, no space left on the device?
But you don't know which folder is consuming the most space?
Bash script below will get all the folder size and display the size for each folder.
How to use the script below?
Open your favourite terminal editor, vi, vim or other editor.
At terminal, type: vi dirfoldersize.sh
Copy and paste the code below to the editor.
Save the file.
Then make the script executable, chmod +x dirfoldersize.sh
Run the script by typing, ./dirfoldersize.sh
If everything goes well, you will get the folder size for each folder on the directory where the script runs. See sample output below.
=============== #code begins start copy below
#!/bin/bash curdir=$(pwd) compath="$curdir/dirlist.txt" ls -l | grep ^d | awk '{print $9}'  > $compath

while IFS= read -r var do

  foldersize=$(du -sh $var |awk '{print $1}')
  echo "$curdir/$var folder size: $foldersize"
done < "$compath"
#code ends, end copying from above line ======…

List completed cron jobs in Centos

Listing cron jobs or log files within a specific time frame is quite hard especially if the log or logs are quite a big file.

But of course, doing the lazy way but a smarter way is always a good option.

Use SED or stream editor.

In Centos the log is in: /var/log/cron

/var/log - path for the file
 cron - is the file that keeps the record for cron jobs, there is no filename extension

To check the logs within the 24 hours time, sed can do it easily and quickly.

Here's one line, time saver command to check the cron log file:

sed -n '/Mar 10 00:00:01/ , /Mar 11 00:01:01/p' /var/log/cron

To redirect the output to a file:

sed -n '/Apr 10 00:00:01/ , /Apr 11 00:01:01/p' /var/log/cron > cron24_hours.record.log

You can replace /var/log/cron with any other files as long as it follows the time format of Month, Day of the month and the time in HR:MM:SS format.


Download the free Linux Android App cheat sheet, see link below. It's free. Enjoy.



Cheers..till next time!

=======…

Linux bash script copy folders and files from list

In Linux special characters such as "#", "-" and other special characters cannot be access directly either by manual copy or via script. For example this path below it has the sharp "#" key:
/usr/dfiles/$line/Tasks/#msgs/
cp "/usr/dfiles/$line/Tasks/#msgs/*.eml" "/mnt/NAS/Tasks1/line/"
Even though the path is enclosed by quotation marks, Linux system will show "No such file or directory".

Since the system is unable to find the "#msgs" directory.

As a work around in this issue is to append "--" double dash before the cp command and everything will work fine.

So, this command below will work.

cp  -- "/usr/dfiles/$line/Tasks/#msgs/*.eml" "/mnt/NAS/Tasks1/line/"
Copying one folder is quite practical to do it by hand rather by script.
But if you are copying hundreds or thousands of folders, doing it manually is quite painful.
To copy hundreds or thousands of folders to another folder the pra…

Linux protect file from deletion

To protect file from deletion in Linux system, the chattr command is able to set the attributes that protects the file.

chattr +a my_protected_file.txt

chattr +a  == means that the file can be appended and the file can't be deleted as well.

To set the file to immutable, "+i"  attribute can be used.

chattr +i the_protected_file.txt

Immutable file is protected from deletion and the original contents of the file is also preserved because no changes can be made.

This command below:

ls xx*.txt >> the_protected_file.txt

The command above will show "permission denied" if the file is immutable.

If the file is set with +a, then the above command will append the output of "ls" to the file.

To unset or removed the attribute use the minus sign, "-a", "-i".

For example, chattr -a the_protected_file.txt or  chattr -i the_protected_file.txt

To know more about chattr type the command below:

man chattr
chattr --help
info chattr


This wiki link shows …

Linux search string in text files

Search a string or a pattern in text files without opening the file.

Grep is a handy tool to find or search a string in text files.

Grep is an available tool in Linux and Unix OSes.

So if you have some data or information stored in text files and forget where the file is located.

As long as you know the keyword or a string to search for, then grep and find command will be your utmost friend.

"Grep" and "find" are tools to make life easier to get the information you want but how to use it?

Command below will search recursively in the patch specified for all the text files and display the file where the match is found, output will also include the path and the filename.

find /home/00_Notes -name '*.txt' -print0 | xargs -0r grep -H 'vanity baseline'



The xargs -0r,  is zero r.

The above command will search recursively in all folders and subfolders for text files which contains the string "vanity baseline".

If a match is found an output will be…

Searching files in Linux

Search the whole directory:

    find / -name "php.ini" -print

    find / -name "owncloud.conf" -print


Using whereis command:

  whereis php.ini

From man whereis (command):
NAME
       whereis  -  locate the binary, source, and manual page files for a com-
       mand



Using locate command:

locate php.ini

From man locate (command):
NAME
       locate - find files by name



To find out more, type at terminal:

    info locate
    info whereis

    locate --help
    whereis --help



Cheers..till next time.

================================ Free Android Apps:
Click on links below to find out more:
Linux Android App cheat sheet: https://play.google.com/store/apps/details?id=com.LinuxMobileKit
Multiplication Table for early learners https://play.google.com/store/apps/details?id=com.TableMultiplication

https://play.google.com/store/apps/details?id=soulrefresh.beautiful.prayer
Catholic Rosary Guide  for Android:
https://play.google.com/store/apps/details?id=com.myrosaryapp
http://quickbytesstuff.blogspot…

Linux Awk insert text at specific line number

One liner command for Awk to insert a new string or text at a specific line number on a file.

awk 'NR==3{print "new line text at row 3"}1' original_file.txt > modified_file.txt


'NR==3 --insert string or data at line number 3
"}1' -- number 1 (can be any number) means to append the new text or string
> modified_file.txt -- redirect the output to a new file


To find out more, type this at the terminal.

                info awk
                info print
                man awk
                man print


Or you can redirect the output of info and man to a file for offline viewing.


Check out links below for other examples of awk command.

http://quickbytesstuff.blogspot.sg/2016/02/linux-awk-simple-example.html

http://quickbytesstuff.blogspot.sg/2016/05/basic-awk-usage-tutorial.html

Tools for Linux Troubleshooting

A basic troubleshooting skill is just necessary for a System Administrator. One way or another thing will go south, no matter how you make sure that the system is working perfectly healthy.
Patches, updates, new security holes or vulnerabilities will definitely change the whole system and if something goes wrong, then a working system will need troubleshooting.
Even new system configuration changes that are not recorded and left forgotten and cause an adverse effect that is applied to the system will cause chaos and more time will be spent in troubleshooting, only to find out that a minor change causes the whole thing.
Here are some tools below, which could provide a basic troubleshooting to a Linux system.
A general purpose logs files, which shows messages from the system.
    tail -f /var/log/messages
/var/log/  -- directories which contains log files for the system. This directory can easily fill up the space of the whole system if not the log files are not managed properly.
/etc/rsy…