{"id":43,"date":"2009-01-10T08:10:44","date_gmt":"2009-01-10T08:10:44","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=43"},"modified":"2015-03-12T06:02:05","modified_gmt":"2015-03-12T06:02:05","slug":"linux-shell-commands-quick-how-to","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/linux-shell-commands-quick-how-to","title":{"rendered":"Linux Shell Commands &#8211; quick how-tos"},"content":{"rendered":"<p>Following are linux commands, tricks for doing regular tasks.<\/p>\n<h1>Rename files: replace space by _ in linux:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>\r\nfor files in *; do mv \"$files\" `echo $files | tr ' ' '_'`; done\r\n<\/pre>\n<\/blockquote>\n<h1>Change case of multiple file names:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>for files in *; do mv $files `echo $files | tr '[:upper:]' '[:lower:]'`; done<\/pre>\n<\/blockquote>\n<h1>Find files having large size:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>ls -hlRtr | awk '{print $5 \" \" $6 \"-\" $7 \" \"$9}' | grep '[0-9]G ' <\/pre>\n<\/blockquote>\n<p>&#8211; Variants: grep &#8216;[0-9][0-9][0-9]M&#8217;  will give you files having sizes xxxMBs.<\/p>\n<h1>Find and replace text from multiple files :<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find .\/ -type f | xargs perl -pi -w -e 's\/SEARCH-TERM\/REPLACE-TERM\/g;'<\/pre>\n<\/blockquote>\n<h1>Find text in file:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find . -name \"*.php\" | xargs grep \" order by l.ID \"<\/pre>\n<\/blockquote>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find . -name \"*.pl\" | xargs grep \"virtualTourUrl\"<\/pre>\n<\/blockquote>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>grep *.* \"Pattern\"<\/pre>\n<\/blockquote>\n<h1>Find and Delete file within last one hour:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find \/some\/dir -type f -mmin +60 | xargs rm -f<\/pre>\n<\/blockquote>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find . -cmin +60 -exec rm -f {} \\;<\/pre>\n<\/blockquote>\n<h1>Find files last modified \/ accessed &#8211; copy to other directory:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find . -mtime +1 -exec cp {} TARGET_FOLDER \\;<\/pre>\n<\/blockquote>\n<p>&#8211; atime..last access<br \/>\n&#8211; mtime..last modify<br \/>\n&#8211; ctime..last status change<\/p>\n<h1>Replace content of a file:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>perl -pi -e \"s\/kedar\/radek\/g\" a.txt<\/pre>\n<\/blockquote>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>perl -pi -e \"s\/OLDTEXT=OLDVALUE\/NewText=NewValue\/g\" *.*\r\n<\/pre>\n<\/blockquote>\n<h1>Find a.txt and Replace content by string comparison and save in to other file:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find .\/ -name \"a.txt\" -exec sed 's\/radek\/kedar\/g' {} \\; &gt; b.txt\r\n<\/pre>\n<\/blockquote>\n<h1>Find files modified in last n days:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>find *.sql -mtime +n -exec ls -l --sort=time {} \\;\r\n<\/pre>\n<\/blockquote>\n<h1>Create and Extract a Tar file:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>tar -cvf \/path\/to\/file.tar \/path\/to\/source\/ archived<\/pre>\n<\/blockquote>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>tar -xvf \/path\/to\/file.tar\r\n<\/pre>\n<\/blockquote>\n<h1>A quick sftp \u2013 secure file transfer program to transfer files between linux\/unix machines:<\/h1>\n<blockquote style=\"background-color:beige\">\n<pre style=white-space:pre-wrap>sftp usename@hostname\/ipaddress\r\npassword: ****\r\nsftp&gt; ls #executed on remote machine\r\nsftp&gt; lls #executed on local machine\r\nsftp&gt; get *.* #download all files from remote machine to local machine\r\nsftp&gt; put *.* #upload files to remote machine\r\n\r\n- Create directory in sftp: mkdir [For local: lmkdir]\r\n- Change directory in sftp: cd [For local: lcd]\r\n- get \/ mget : download files to local linux machine\r\n- put \/ mput : upload files to remote linux machie\r\n<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"Following are linux commands, tricks for doing regular tasks. Rename files: replace space by _ in linux: for files in *; do mv &#8220;$files&#8221; `echo $files | tr &#8216; &#8216;&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[25,59,172,426],"class_list":{"0":"post-43","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-technical","7":"tag-command","8":"tag-linux","9":"tag-linux-commands","10":"tag-technical"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":3,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":2139,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/43\/revisions\/2139"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}