למדו למה 'פחות' מהיר יותר מהפקודה 'יותר' לניווט בקבצים

הפקודה More היא פקודת שורת פקודה שמשמשת להצגת תוכן של קובץ במסוף. השימוש הבסיסי בפקודת more הוא להריץ את הפקודה על קובץ כפי שמוצג למטה:

קרא גם: למד את ההבדל בין פקודות ‘cat’ ו-‘tac’ עם דוגמאות

למד על הפקודה ‘more’ ב-Linux

# more /var/log/auth.log
View Contents of auth.log File
Apr 12 11:50:01 tecmint CRON[6932]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 11:50:01 tecmint CRON[6932]: pam_unix(cron:session): session closed for user root
Apr 12 11:55:01 tecmint CRON[7159]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 11:55:01 tecmint CRON[7160]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 11:55:01 tecmint CRON[7160]: pam_unix(cron:session): session closed for user root
Apr 12 11:55:02 tecmint CRON[7159]: pam_unix(cron:session): session closed for user root
Apr 12 12:00:01 tecmint CRON[7290]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:00:01 tecmint CRON[7290]: pam_unix(cron:session): session closed for user root
Apr 12 12:05:01 tecmint CRON[7435]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:05:01 tecmint CRON[7436]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:05:01 tecmint CRON[7436]: pam_unix(cron:session): session closed for user root
Apr 12 12:05:02 tecmint CRON[7435]: pam_unix(cron:session): session closed for user root
Apr 12 12:09:01 tecmint CRON[7542]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:09:01 tecmint CRON[7542]: pam_unix(cron:session): session closed for user root
Apr 12 12:10:01 tecmint CRON[7577]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:10:01 tecmint CRON[7577]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 tecmint CRON[7699]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:15:01 tecmint CRON[7700]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:15:01 tecmint CRON[7700]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 tecmint CRON[7699]: pam_unix(cron:session): session closed for user root
....

דרך נוספת להשתמש בפקודת more היא בשימוש (צנרת) עם פקודות אחרות, כמו פקודת cat, כפי שמוצג בדוגמה למטה:

# cat /var/log/auth.log | more
View and Navigate Contents of File

כדי לנווט בקובץ שורה אחר שורה לחץ על מקש Enter או לחץ על מקש Spacebar כדי לנווט דף אחד בכל פעם, כאשר הדף הוא גודל המסך הנוכחי שלך. כדי לצאת מהפקודה פשוט לחץ על המקש q.

A useful option of more command is the -number switch which allows you to set the number of line a page should contain. As an example display the auth.log file as a page of 10 lines:

# more -10 /var/log/auth.log
Show Only First 10 Lines of File

בנוסף, ניתן להציג דף המתחיל ממספר שורה מסוים באמצעות האפשרות +מספר כפי שמוצג למטה:

# more +14 /var/log/auth.log
Show Only First 14 Lines of auth.log File
Apr 12 12:09:01 tecmint CRON[7542]: pam_unix(cron:session): session closed for user root
Apr 12 12:10:01 tecmint CRON[7577]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:10:01 tecmint CRON[7577]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 tecmint CRON[7699]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:15:01 tecmint CRON[7700]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:15:01 tecmint CRON[7700]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 tecmint CRON[7699]: pam_unix(cron:session): session closed for user root
Apr 12 12:16:01 tecmint mate-screensaver-dialog: gkr-pam: unlocked login keyring
Apr 12 12:17:01 tecmint CRON[7793]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:17:01 tecmint CRON[7793]: pam_unix(cron:session): session closed for user root
Apr 12 12:20:01 tecmint CRON[7905]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:20:01 tecmint CRON[7905]: pam_unix(cron:session): session closed for user root
Apr 12 12:25:01 tecmint CRON[8107]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:25:01 tecmint CRON[8108]: pam_unix(cron:session): session opened for user root by (

למד על הפקודה ‘less’ ב-Linux

דומה ל-more, פקודת less מאפשרת לך להציג את תוכן הקובץ ולנווט בו. ההבדל העיקרי בין more ו-less הוא שהפקודה less מהירה יותר מפני שהיא לא טוענת את כל הקובץ בפעם אחת ומאפשרת ניווט בקובץ באמצעות מקשי עלה/למטה.

ניתן להשתמש בפקודה זו כפקודה עצמאית שניתן להנפיק נגד קובץ או להשתמש בצינורות עם מספר רב של פקודות Linux כדי להצמיד את פלט המסך שלהם ולאפשר לך לגלול דרך התוצאות.

# less /var/log/auth.log
# ls /etc | less

ניתן לנווט בקובץ שורה אחר שורה על ידי לחיצה על מקש Enter. ניווט בעמוד ניתן לנהל בעזרת מקש spacebar. גודל העמוד מיוצג על ידי גודל המסך הנוכחי שלך. כדי לצאת מהפקודה הקלד את המקש q, בדיוק כמו בפקודת more.

A useful feature of less command is the use of /word-to-seach option. For instance you can search and match all sshd messages from a log file by interactively specifying the /sshd string.

View File Content Using less Command

כדי להציג קובץ מתחילת שורה מסוימת, השתמש בתחביר הבא:

# less +5 /var/log/auth.log

אם נדרש לעקוב אחר מספר כל שורה עם פקודת less השתמש באפשרות -N.

# less -N /var/log/daemon.log
Show Number for Every Line in File
      1 Apr 12 11:50:01 tecmint CRON[6932]: pam_unix(cron:session): session opened for user root by (uid=0)
      2 Apr 12 11:50:01 tecmint CRON[6932]: pam_unix(cron:session): session closed for user root
      3 Apr 12 11:55:01 tecmint CRON[7159]: pam_unix(cron:session): session opened for user root by (uid=0)
      4 Apr 12 11:55:01 tecmint CRON[7160]: pam_unix(cron:session): session opened for user root by (uid=0)
      5 Apr 12 11:55:01 tecmint CRON[7160]: pam_unix(cron:session): session closed for user root
      6 Apr 12 11:55:02 tecmint CRON[7159]: pam_unix(cron:session): session closed for user root
      7 Apr 12 12:00:01 tecmint CRON[7290]: pam_unix(cron:session): session opened for user root by (uid=0)
      8 Apr 12 12:00:01 tecmint CRON[7290]: pam_unix(cron:session): session closed for user root
      9 Apr 12 12:05:01 tecmint CRON[7435]: pam_unix(cron:session): session opened for user root by (uid=0)
     10 Apr 12 12:05:01 tecmint CRON[7436]: pam_unix(cron:session): session opened for user root by (uid=0)
     11 Apr 12 12:05:01 tecmint CRON[7436]: pam_unix(cron:session): session closed for user root

כברירת מחדל הדרך היחידה לצאת מהפקודה less היא ללחוץ על המקש q. כדי לשנות את ההתנהגות ולצאת אוטומטית מהקובץ בעת הגעתך לסופו, השתמש באפשרות -e או -E:

# less -e /var/log/auth.log
# less -E /var/log/auth.log

כדי לפתוח קובץ בהתאמה להתאמה ראשונה של תבנית, השתמש בתחביר הבא:

# less +/sshd /var/log/auth.log
Show Given Matching String in File
Apr 12 16:19:39 tecmint sshd[16666]: Accepted password for tecmint from 192.168.0.15 port 41634 ssh2
Apr 12 16:19:39 tecmint sshd[16666]: pam_unix(sshd:session): session opened for user tecmint by (uid=0)
Apr 12 16:19:39 tecmint systemd-logind[954]: New session 1 of user tecmint.
Apr 12 16:19:48 tecmint sshd[16728]: Received disconnect from 192.168.0.15: 11: disconnected by user
Apr 12 16:19:48 tecmint sshd[16666]: pam_unix(sshd:session): session closed for user tecmint
Apr 12 16:20:01 tecmint CRON[16799]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 16:20:02 tecmint CRON[16799]: pam_unix(cron:session): session closed for user root
Apr 12 16:25:01 tecmint CRON[17026]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 16:25:01 tecmint CRON[17025]: pam_unix(cron:session): session opened for user root by (uid=0)

הפקודה לעיל מסבירה ל־less לפתוח את קובץ auth.log בהתאמה להתאמה ראשונה של מחרוזת sshd.

כדי להוסיף באופן אוטומטי את תוכן הקובץ שנפתח בפקודת less השתמש בשילוב מקשי Shift+f או הפעל את less בעזרת התחביר הבא.

# less +F /var/log/syslog

פעולה זו מאפשרת ל־less לרוץ במצב אינטראקטיבי (חי) ולהציג תוכן חדש בזמן אמת בזמן שממתינים לנתונים חדשים להיות כתובים לקובץ. ההתנהגות הזו דומה לפקודת tail -f.

בשילוב עם דפוס ניתן לצפות בקובץ הלוג באופן אינטראקטיבי עם מקש Shift+f בזמן שהוא מתאים את מילת המפתח. כדי לצאת ממצב החי פשוט ללחוץ על מקשי Ctrl+c. אם תחליט להשתמש

# less +/CRON /var/log/syslog

יותר או פחות, שהוא בחירה אישית, תזכור שפחות הוא יותר עם יותר תכונות.

קרא גם: ניהול קבצים באופן יעיל באמצעות פקודות head, tail ו-cat

Source:
https://www.tecmint.com/linux-more-command-and-less-command-examples/