Linux commands: Difference between revisions

Jump to navigation Jump to search
115 bytes added ,  22 January 2018
Line 6: Line 6:
: {{Win}}: Type {{kbd | key= ''command_name'' <nowiki>/?</nowiki>}} "Open Source tools [http://www.cygwin.com/ Cygwin] which provide functionality similar to a Linux distribution on Windows"
: {{Win}}: Type {{kbd | key= ''command_name'' <nowiki>/?</nowiki>}} "Open Source tools [http://www.cygwin.com/ Cygwin] which provide functionality similar to a Linux distribution on Windows"


== text file manipulation ==
== Text file manipulation ==
display the first lines of text file
=== Display the first lines of text file ===
* [http://en.wikipedia.org/wiki/Head_(Unix) head] <nowiki>[options]</nowiki> ''filename'' ({{OS}}: {{Linux}})
* [http://en.wikipedia.org/wiki/Head_(Unix) head] <nowiki>[options]</nowiki> ''filename'' ({{OS}}: {{Linux}})
* [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' (and press gg) ({{Linux}})
* [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename'' (and press gg) ({{Linux}})
* [http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (and press gg) ({{Linux}}); Open file and press gg for [http://www.vim.org/ gVim] ({{Win}})
* [http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (and press gg) ({{Linux}}); Open file and press gg for [http://www.vim.org/ gVim] ({{Win}})


display the last few lines of text file
=== Display the last few lines of text file ===
* [http://en.wikipedia.org/wiki/Tail_(Unix) tail] <nowiki>[options]</nowiki> ''filename'' ({{Linux}})
* [http://en.wikipedia.org/wiki/Tail_(Unix) tail] <nowiki>[options]</nowiki> ''filename'' ({{Linux}})
* [http://unix.stackexchange.com/questions/47407/cat-line-x-to-line-y-on-a-huge-file tail - cat line X to line Y on a huge file - Unix & Linux Stack Exchange]
* [http://unix.stackexchange.com/questions/47407/cat-line-x-to-line-y-on-a-huge-file tail - cat line X to line Y on a huge file - Unix & Linux Stack Exchange]
Line 19: Line 19:
* [http://tailforwin32.sourceforge.net/ Tail for Win32] GUI interface ({{Win}}) {{exclaim}} not support Unicode file for v.4.2.6
* [http://tailforwin32.sourceforge.net/ Tail for Win32] GUI interface ({{Win}}) {{exclaim}} not support Unicode file for v.4.2.6


Display the content of text file
=== Display the entire content of text file ===
* [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename''  ({{Linux}})
* [http://en.wikipedia.org/wiki/Vi vi] <nowiki>[options]</nowiki> ''filename''  ({{Linux}})
* {{kbd | key=<nowiki>cat <filename></nowiki>}}  ({{Linux}})
* {{kbd | key=<nowiki>cat <filename></nowiki>}}  ({{Linux}})
* {{kbd | key=<nowiki>type <filename></nowiki>}}  ({{Win}})
* {{kbd | key=<nowiki>type <filename></nowiki>}}  ({{Win}})


Display the partial content of large text file page by page
=== Display the partial content of large text file page by page ===
* {{kbd | key=<nowiki>cat <filename> | less</nowiki>}}  ({{Linux}} or Cygwin on {{Win}}) {{exclaim}} Chinese issue<ref>[https://stackoverflow.com/questions/6943928/show-special-characters-in-unix-while-using-less-command linux - Show special characters in Unix while using 'less' Command - Stack Overflow] ''un-verified''</ref>
* {{kbd | key=<nowiki>cat <filename> | less</nowiki>}}  ({{Linux}} or Cygwin on {{Win}}) {{exclaim}} Chinese issue<ref>[https://stackoverflow.com/questions/6943928/show-special-characters-in-unix-while-using-less-command linux - Show special characters in Unix while using 'less' Command - Stack Overflow] ''un-verified''</ref>
* {{kbd | key=<nowiki>type <filename> | more</nowiki>}}  ({{Win}}) If the file was encoded in UTF-8, you need to key in {{kbd | key=chcp 65001}} first <ref>[http://fatting10.blogspot.tw/2015/08/solved-window.html (Solved) Window命令視窗中文亂碼 改編碼方法]</ref>.
* {{kbd | key=<nowiki>type <filename> | more</nowiki>}}  ({{Win}}) If the file was encoded in UTF-8, you need to key in {{kbd | key=chcp 65001}} first <ref>[http://fatting10.blogspot.tw/2015/08/solved-window.html (Solved) Window命令視窗中文亂碼 改編碼方法]</ref>.


newline count (count the number of lines of a file), word count
=== Newline count (count the number of lines of a file), word count ===
* {{kbd | key= <nowiki>wc -l <filename></nowiki>}} for {{Linux}} <ref>[https://en.wikipedia.org/wiki/Wc_(Unix) wc (Unix) - Wikipedia, the free encyclopedia]</ref> ex: {{kbd | key= <nowiki>wc -l *.txt</nowiki>}} to print the newline count of txt files of the current folder  
* {{kbd | key= <nowiki>wc -l <filename></nowiki>}} for {{Linux}} <ref>[https://en.wikipedia.org/wiki/Wc_(Unix) wc (Unix) - Wikipedia, the free encyclopedia]</ref> ex: {{kbd | key= <nowiki>wc -l *.txt</nowiki>}} to print the newline count of txt files of the current folder  
* {{kbd | key= <nowiki>find . -type f -exec cat {} + | wc -l</nowiki>}} for {{Linux}} & {{Mac}} to print the newline count of all files on all subdirectories<ref>[https://stackoverflow.com/questions/13727917/use-wc-on-all-subdirectories-to-count-the-sum-of-lines linux - Use wc on all subdirectories to count the sum of lines - Stack Overflow]</ref>
* {{kbd | key= <nowiki>find . -type f -exec cat {} + | wc -l</nowiki>}} for {{Linux}} & {{Mac}} to print the newline count of all files on all subdirectories<ref>[https://stackoverflow.com/questions/13727917/use-wc-on-all-subdirectories-to-count-the-sum-of-lines linux - Use wc on all subdirectories to count the sum of lines - Stack Overflow]</ref>
* {{kbd | key= <nowiki>wc -l "$(cygpath -u '<path to file>')"</nowiki>}} for cygwin on {{Win}} ex: {{kbd | key= <nowiki>wc -l "$(cygpath -u 'c:\Program Files\file.txt')"</nowiki>}}<ref>[[Cygwin convert windows path to linux path]]</ref>
* {{kbd | key= <nowiki>wc -l "$(cygpath -u '<path to file>')"</nowiki>}} for cygwin on {{Win}} ex: {{kbd | key= <nowiki>wc -l "$(cygpath -u 'c:\Program Files\file.txt')"</nowiki>}}<ref>[[Cygwin convert windows path to linux path]]</ref>


Save a Unix manpage as plain text
=== Save the console message as text file ===
Save the Unix manpage as plain text file
* {{kbd | key= <nowiki>man man | col -b > man.txt</nowiki>}} Thanks! [http://www.electrictoolbox.com/article/linux-unix-bsd/save-manpage-plain-text/ Save a Unix manpage as plain text]
* {{kbd | key= <nowiki>man man | col -b > man.txt</nowiki>}} Thanks! [http://www.electrictoolbox.com/article/linux-unix-bsd/save-manpage-plain-text/ Save a Unix manpage as plain text]


Search text in a file
=== Search text in a file ===
* grep command for multiple files: {{kbd | key=<nowiki>grep -ir "string to search" /path/to/directory</nowiki>}} or {{kbd | key= grep ''keyword'' /path/to/file}}  ({{Linux}})
* grep command for multiple files: {{kbd | key=<nowiki>grep -ir "string to search" /path/to/directory</nowiki>}} or {{kbd | key= grep ''keyword'' /path/to/file}}  ({{Linux}})
* vim: (1)[http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (2)press {{kbd | key= /keyword}}  ({{Linux}})
* vim: (1)[http://www.tuxfiles.org/linuxhelp/vimcheat.html vim] <nowiki>[options]</nowiki> ''filename'' (2)press {{kbd | key= /keyword}}  ({{Linux}})
Line 46: Line 47:
[[Desktop_search#Search_text_in_files| Search text in files]]<ref>[http://linux.vbird.org/linux_basic/0320bash.php#col 鳥哥的 Linux 私房菜 -- 學習 bash shell] </ref>
[[Desktop_search#Search_text_in_files| Search text in files]]<ref>[http://linux.vbird.org/linux_basic/0320bash.php#col 鳥哥的 Linux 私房菜 -- 學習 bash shell] </ref>


Merger multiple plain text files
=== Merge multiple plain text files ===
* {{kbd | key=<nowiki>copy *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>copy file1.txt file2.txt > bundle.txt </nowiki>}} for  {{Win}}
* {{kbd | key=<nowiki>copy *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>copy file1.txt file2.txt > bundle.txt </nowiki>}} for  {{Win}}
* {{kbd | key=<nowiki>cat *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>cat file1.txt file2.txt > bundle.txt </nowiki>}} for {{Linux}}<ref>[http://www.maclife.com/article/columns/terminal_101_join_multiple_files_together_cat Terminal 101: Join Multiple Files Together with Cat]</ref><ref>[https://unix.stackexchange.com/questions/3770/how-to-merge-all-text-files-in-a-directory-into-one How to merge all (text) files in a directory into one? - Unix & Linux Stack Exchange]</ref>
* {{kbd | key=<nowiki>cat *.txt > bundle.txt </nowiki>}} or {{kbd | key=<nowiki>cat file1.txt file2.txt > bundle.txt </nowiki>}} for {{Linux}}<ref>[http://www.maclife.com/article/columns/terminal_101_join_multiple_files_together_cat Terminal 101: Join Multiple Files Together with Cat]</ref><ref>[https://unix.stackexchange.com/questions/3770/how-to-merge-all-text-files-in-a-directory-into-one How to merge all (text) files in a directory into one? - Unix & Linux Stack Exchange]</ref>

Navigation menu