Skip to main content

Posts

Showing posts with the label Copy selected files in Linux

Bash slice string in Linux Shell

Strings are quite basic in any scripting or programming languages.If a journey to a thousand miles starts with a single step, in the programming world the journey starts with a string called “Hello World” and beyond “Hello World” pseudocode and algorithm will keep the journey going. Strings in scripting or programming are just group of characters.So, if string is a group of characters then it can be slice by character or a sub-string can be taken from the string.To slice a string in Bash, a syntax which is part of the Bash library can be used.Syntax is: ${string_to_be_sliced:slice_start_position:slice_end_position}Example string: slicethestring=“Hello Algorithm World”The string is stored in a variable: slicethestringTo get the sub-string “Algorithm World”.Code will be:echo ${slicethestring:6:23} #start the slice at position 6 till position 23Another way to slice the string from a specified start position till the end; is to tell Bash the start position and just omit the end position.O…

Linux sed remove duplicates and get unique values

Removing duplicates and getting unique values is quite easy provided that the input data follows a specific format, for example the string or raw data has spaces in between.
But a dilemma can occur if the data has no spaces in between the characters of the string, instead of spaces it is separated by dashes.
So, how to remove duplicates, get the unique values and still retain the format of the raw data?
Like this raw data: (just a sample string) the-quick-brown-fox-jumps-over-the-lazy-dog-jumps-over-the-lazy-cat-jumps-over-the-rabbit
When removing duplicates and getting unique values via this command:
sort duplicates.txt | uniq (this will work if the data is separated by spaces)
duplicates.txt assumes that it has the string as illustrated above.
Sample output:

The output will be the exactly be the same with the input. Why? It is because the whole string is treated as literal one string, because the dashes connect between the character eliminating the space delimiter.
Example, if the requiremen…

Create a new text file with content in Linux

Touch command in Linux will create an empty file in Linux command line. If no other parameters is specified whether a line will be inserted to the newly created file.
Cat command is for reading files via command line but it can also be used to create a file in Linux terminal window command line.
Cat command below will create a file called “test_comment.txt” and with the content or the line of “Hello World of Touch and Create File” on the file created.
Here’s the shell command:
cat  <( echo “Hello World of Touch and Create File”)  >  test_comment.txt
Note that there is no space between “<(“, if there is a space then the command will not work as expected.
Touch command to create a new file and also insert a new line or insert a string on the newly created file.
Here’s the shell command:
touch touch_file_comment.txt; echo “Hello World of Touch, insert this line.” >  touch_file_comment.txt

Basically, the command executes two commands separated by “;” semi-colon. The first part is to cr…

How to copy a line using Nano editor?

Copying in Nano needs a few keys to copy and paste. 
Copy and paste goes hand in hand, there’s no point to copy if you cannot paste.
So, how to copy using Nano editor in Linux?
To copy in Nano editor , a few key strokes are needed.
On the beginning of the line that will be copied press:
ctrl + 6 (to set a mark)
Press “end” key or use right arrow to go to the end of the line.
If multiple lines are to be copied used down arrow to select the lines.
After selecting the line or lines to be copied, press:
alt + 6 (to set unmark and all line/s selected is copied to the clipboard already.
To copy the line or lines, position the cursor where the line/s will be copied and press:
ctrl + u (this will paste the line/s or whatever is on the clipboard)

That’s it, quite a few key strokes to remember but if you’re in a command line or in Putty; then these key combination to copy and paste in Nano is really a life saver.

Cheers..till next time. :)


================================ Free Android Apps:
Click  link…

Check permission of the current directory in Linux

Checking permission of a current directory in Linux is quite simple and basic.

The "ls" command will do the job.

ls does not require sudo membership to use the command.

Even if a directory has no files on it, just type: ls -lad

This will show the permissions on the current directory where the command is typed. Even if the directory has no files or it is empty it will show the folder permissions.

Consult "man ls" why ls -lad works to check permissions on empty folders.

Typing, ls -ltr won't show anything if the folder is empty.

Typing, ls -lad will show the permission whether the folder is empty. The ls command will display "." the current folder and ". .", basically this will show the hidden files and it's current permission settings.
But by typing ls only or ls -ltr it won't show any permissions if the folder is empty since there are no files to display and thus no information is displayed.


Cheers. Till next time.

==================…

Add a remote folder in Linux

Adding a remote folder in Linux or basically mounting a folder in Linux system is quite easy.
Like in copying and moving files or folder, it requires a source and a destination.
A simple copy command in Linux is:
cp --source or any file to be copied--  --destination or where to copy the file--

In mounting a folder, using mount command does require also a source and a destination. man mount will display the available options for this command.
Since source and destination is a pre-requisite in cp, mv ormount command.
In mounting a remote folder, the destination folder must be ready or created first before typing the mount command.

Linux has default destination for mounting folder which is /mnt folder.
Mounting a remote folder can be also done on other location such /home /usr or other preferred locations.

To mount a folder in /mnt directory, a sub folder must be created first.
To create a folder, type: mkdir /mnt/remote_folder_101

Once the sub folder or the destination folder is created…

Linux folder with special characters

Operating system or even applications programs does restrict some characters since it's either used by the system or it is not permitted for some other reasons.

In Linux special characters like ampersand, dashes and other special characters should not be used as folder names or even file names.

But some users or even IT folks violate this rule without knowing the consequences it holds.

Of course, whatever characters available on the keyboard should be used or else what's the point of having them on the keyboard. :)

But there are consequences in using those mentioned characters.

In Linux terminal, if the folder name has an ampersand on its folder name like the image below it cannot be copied easily.

For example navigating in Linux through the terminal using "cd" should be smooth but sometimes simple things can be tough if you don't know how to get around with it.


Navigating folders with special characters using "cd " command is quite simple by enclosing…

Linux copy files and change date or time

So, you want to copy files from one folder to another folder and change both the source and destination timestamp?
Linux can easily change or modify the timestamp with ‘touch’ command.
A one liner command with the help of ‘pipe’ to pass the arguments and ‘xargs’ to execute multiple commands in a single line can easily accomplish this task.
Command below will copy the files in the current directory where ‘ls’ command is executed to the directory ‘xfiles’, timestamp will be changed to 7 hours less from the current time.
ls | xargs -I % sh -c 'touch -d "7 hours ago" %; cp -p % ./xfiles';
-p option is important to preserve the modified timestamp
Note: the above command will change the timestamp for both source files and destination files.
Command below will change the source timestamp, but the destination timestamp will be the date and time that the command was executed.
ls | xargs -I % sh -c 'touch -d "2 Aug" %; cp % ./x';

Notice, that the -p option is omitted …

Linux list folders recursively

To list all folders on the root directory, type:
  ls -d /*
Sample Output:
/bin     /dev   /smsbackup  /lost+found  /mnt  /proc  /selinux  /tmp /boot    /etc   /lib        /media       /net  /root  /srv      /usr /cgroup  /home  /lib64      /misc        /opt  /sbin  /sys      /var
To list all folders on the current folder, one level depth only; type:
[xxx@mx var]$ ls -d */
Sample Output:
account/  cvs/    games/  local/  lost+found/  opt/       spool/  yp/ cache/    db/     gdm/    lock/  sms/        preserve/  tmp/ crash/    empty/  lib/    log/    nis/         run/       www/

List all folders and sub folders, 2 level depth or 2 level hierarchy; type:
[xxx@mx var]$ ls -d */*/
Sample Output:
cache/abrt-di/        lib/misc/        log/ConsoleKit/      run/portreserve/ cache/cups/           lib/mlocate/     log/cups/            run/rhsm/ cache/fontconfig/     lib/net-snmp/    log/sms/            run/saslauthd/ cache/foomatic/       lib/nfs/         log/gdm/             run/sepermit/ cache…

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!

=======…